- #1
ming2194
- 32
- 0
[urgent] my complete attempt of a very difficult c problem
http://u1.imgupload.co.uk/1258070400/9847_untitled.png
*** i need to write it in C program. (not C++)
i want to know whether all the code are in C language (not C++) and can my work give the conresponding ourput.
i know it maybe too time consuming for someone helping me to check my work.
but i just hope there are some kind ppl can rly help me.
thx by heart.
Homework Statement
http://u1.imgupload.co.uk/1258070400/9847_untitled.png
Homework Equations
*** i need to write it in C program. (not C++)
The Attempt at a Solution
PHP:
#include <stdio.h>
#include <math.h>
#define N 368
int main ()
{
int i, j, s;
int is_prime[N]; /* if is_prime[i] == 0, then i isn't prime */
int prime_count = 0;
/* initially, all are presumed prime */
printf ("\nThe prime numbers less than of equal to %d are:\n", N);
for (i=0; i<N; i++) is_prime[i] = 1;
/* find the limit of the numbers to count up to */
s = (int) sqrt (N) + 1;
/* for each i in 2..sqrt(N), mark out multiples of i */
for (i=2; i<s; i++) {
/* mark out 2i, 3i, 4i, ... */
for (j=2*i; j<N; j+=i) is_prime[j] = 0;
}
/* print out the numbers that are left */
for (i=2; i<N; i++){
if (is_prime[i]){
printf ("\t%d\n", i);
prime_count++;
}
}
printf("There total count of prime numbers is: %d\n\n", prime_count);
#define MAX_ARRAY_SIZE 1000
bool looping = true;
while(looping)
{ //Keep repeating until "looping = false"
int number = 0;
printf("\nEnter 3-digit positve integer: ");
scanf("%d", &number);
if (number >= MAX_ARRAY_SIZE)
{
printf("\nError: Number out of range");
return 1; //Return 1 in main() signals an abnormal exit.
}
//**********************************************************************
//Prime Calculation Starts here
int primes[MAX_ARRAY_SIZE];
//This is a for loop, it will repeat the code until i <= number (i is less then or equal to number)
//Add all the numbers 2..n to the vector "primes"
int count = 0;
for(int i = 0; i <= number; ++i)
{
primes[i] = i;
count += 1;
}
primes[count] = -1;
primes[0] = -2;
primes[1] = -2;
int p = 2;
int pIndex = 2;
//All this means is go through every item in the vector "primes"
while( p * p < number ) //Repeat until p^2 is greater then number
{
for(int i = p; primes[i] != -1; ++i)
{
if( primes[i] % p == 0 && primes[i] != p)
{
primes[i] = -2;
}
}
do
{
pIndex += 1;
}
while(primes[pIndex] == -2);
p = primes[pIndex];
}
//**********************************************************************
printf("\nThe prime numbers less than or equal to %d are:\n", number);
int pos = 0;
int pcount = 0;
while(primes[pos] != -1)
{
if(primes[pos] != -2)
{
printf("\t%d\n", primes[pos]);
pcount++;
}
pos++;
}
printf("The total count of prime numbers is: %d\n", pcount);
bool validchoice = false;
while(!validchoice) //The ! means boolean NOT, aka false becomes true, true becomes false.
{
char choice;
printf("\nWould you like to enter the number again? (y/n): ");
scanf("%s", &choice);
if(choice == 'y')
{
validchoice = true;
}
else if( choice == 'n')
{
printf("\n<<<<< Program Completed >>>>>\n\n");
validchoice = true;
return 0;
}
}
}
}
i want to know whether all the code are in C language (not C++) and can my work give the conresponding ourput.
i know it maybe too time consuming for someone helping me to check my work.
but i just hope there are some kind ppl can rly help me.
thx by heart.