![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jun 2005
Posts: 68
Rep Power: 4
![]() |
Creating a program to test a program
After a long absence from these boards and some more programming under my belt, I have finnally dived into C because I have my first college C class this semester. I just finished writing my first C program other than the classic "hello world". It is a program that asks for user input of an integer and decided whether or not the integer is a prime number. This program is no where near efficient and I plan on narrowing down its testing phase to only include numbers that could possibly be a prime (i.e. nothing higer than the root of the input) I would like to write a program that will test this program for each interger up to a stopping point. Also any other efficiency suggestions would be apprecieated.
/****************************************************
*
* Programmer and Purdue Email Address:
* 1.Chris Cadwallader
*
* Program Description: This program asks for user
* input of an integer an outputs whether the
* integer is a prime number or not.
*
*
***************************************************/
#include <stdio.h>
int main()
{
/* Local Definitions */
int testval; // Integer to be tested
int denom; // Denominator used in calculations
int testresult; // Calculation result
int counter; // Loop iteration counter
/* Statements */
/* Requesting an integer */
printf("\n\nWelcome to the Prime number testing program.");
printf("\n\nPlease Enter an integer to be tested: ");
scanf("%d", &testval);
/* Verifying that the input is an integer greater than 1 */
while (testval < 1)
{
printf("\nThe test value cannot be smaller than 1.\n\n");
printf("\n\nPlease enter an integer to be tested: ");
scanf("%d", &testval);
} /* while */
if (testval == 2)
printf("The value entered is a prime number.\n\n");
else
{
denom = 2;
testresult = testval % denom;
counter = 1;
/* Testing integer for prime status */
while (denom < testval)
{
testresult = testval % denom;
if (testresult == 0)
denom = testval;
else
counter++;
denom++;
} /* while */
if (testresult != 0)
printf("The value entered is a prime number.\n\n");
else
printf("The value entered is not a prime number.\n\n");
} /* else */
printf("This program looped %d times to complete the problem.\n\n",counter);
return 0;
} /* main */thanks ps..... my posts seem to have dissappeared.... now I only have 2..... thats strange. |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
You don't need to write a separate program to test your program. You can ask the user for a number under which you want to test the number for being prime. then you can have another loop that iterates from 3 to num and check each for being prime(you can increment the counter by 2 as only odd numbers are prime).
and for your posts disappearing I think you only posted in Lounge as the post count in Lounge has been disabled.
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#3 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Also, at a guess, I'd say that sixstringartist knows a thing or two about finding primes already. As he correctly remarks, you only need to test up to the square root of the number to check if it is prime or not, which suggests that he's already familiar with how to efficiently test for primes. So I'm not sure you need to give him advice there ![]() |
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Jun 2005
Posts: 68
Rep Power: 4
![]() |
Im afraid Im not exactly sure what you mean by platform so I will ramble off some things and hope it helps.
The previous program was written in C, using a UNIX command shell with the vi editor. compiled with gcc in UNIX. |
|
|
|
|
|
#5 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
The first result for "C unit tests" in Google comes up with the Check framework, which seems a good place to start. Check seems to be designed with Linux/Unix in mind, which was why I was asking your platform. There might even be a precompiled Check package for your system. On the Check website, there is tutorial which seems to be fairly straightforward. Typically, these frameworks have a number of tests, sometimes grouped together in one or more test suites so that many tests can be run at once. The frameworks also supply some form of testing the output of functions. For instance, under Check, you have the fail_unless and fail_if functions. Perhaps something like this will work: START_TEST (test_primes) {
fail_unless(your_prime_function(17), "17 should be classed as prime");
fail_if(your_prime_function(6), "6 should be classed as not prime");
}
END_TESTSee if that helps any. |
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
Another way to do it would be to use files. You could have an input file, and save the numbers there. Then you can read from the file and use those numbers in your function. If you really wanted to get fancy, you could make a batch file to run the program and send the output to a file, but that is up to you.
|
|
|
|
|
|
#7 | |
|
Programmer
Join Date: Jun 2005
Posts: 68
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#8 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#9 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
All code in C is encapsulated in a function. The first function called when a program executes is the function called "main". If we take a look at the main function in your code: int main()
{
// Your code
}The second bit in the function is "main", which is the function name. Then "()". This empty pair of brackets tells C that this function has no arguments. Finally, the "{}" brackets encompass your code and tell C where the function starts, and where it ends. A prime function could look like this: #define TRUE 1
#define FALSE 0
int is_prime(int number)
{
int i;
for (i = 2; i < number; i++)
{
if ((number % i) == 0)
{
return FALSE;
}
}
return TRUE;
}We can call this function from main() like so: int main()
{
int input;
printf("Please enter an integer to test: ");
scanf("%d", &input);
if (is_prime(input))
{
printf("That number is prime.\n");
}
else
{
printf("That number is not prime.\n");
}
return 0;
}int function() {
return 0;
}
int main() {
function();
}int main() {
function();
}
int function() {
return 0;
}int function();
int main() {
function();
}
int function() {
return 0;
}Just for reference, a whole prime program could look something like the example I give below. Note it doesn't have the same input checking as yours, but it shouldn't be hard to put that in ![]() // prime.h #ifndef PRIME_H #define PRIME_H int is_prime(int number); #endif // prime.c
#include <stdio.h>
#include "prime.h"
#define TRUE 1
#define FALSE 0
int is_prime(int number)
{
int i;
for (i = 2; i < number; i++)
{
if ((number % i) == 0)
{
return FALSE;
}
}
return TRUE;
}
int main()
{
int input;
printf("Please enter an integer to test: ");
scanf("%d", &input);
if (is_prime(input))
{
printf("That number is prime.\n");
}
else
{
printf("That number is not prime.\n");
}
return 0;
} |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|