C Question

C Question

Page 1 of 1

12 Replies - 2455 Views - Last Post: 22 May 2006 - 11:21 AM Rate Topic: -----

#1 gecheme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 06

C Question

Posted 19 May 2006 - 12:11 PM

Write a C program that will;
• input a positive integer between 1-1000.
• create an integer array whose elements are integers between 1 and the inputted integer.
• change the elements of the array that are prime number into 0.
• create a new array with those prime numbers.
• Output the first array, first array after you modify it and the new prime number array in main.

that is my homework... i have to give it on 23th may..

Is This A Good Question/Topic? 0
  • +

Replies To: C Question

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: C Question

Posted 19 May 2006 - 12:13 PM

Hello,

The site has a policy by which we prefer to see some good faith effort on the part of the user before source code is provided in the matter of academic assignments. If you could please post the code you have written in an effort to solve the problem, our members would be more than happy to guide you.
Was This Post Helpful? 0
  • +
  • -

#3 gecheme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 06

Re: C Question

Posted 19 May 2006 - 12:14 PM

that is what i have done.. it is not giving the correct result..

#include <stdio.h>

int main (void)

{

int i, j, m, n, t, arr[9999], newarr[9999], count;

printf("Enter an integer <1-1000>");
scanf("%d",&count);

printf("First array:");
for(i=0; i<count; i++)
{
arr[i]=i+1;
printf("%3d",arr[i]);
}
for (i=1;i<count;i++);
{
m=0;
for(j=2;j<=arr[i];j++)
{
if(arr[i]%j==0)
m=1;
}

}

if(m==1)

arr[i]=arr[i];

else if(m==0)
n++;
newarr[i]=arr[i];

arr[i]=0;


printf("\nSecond array:");

for(i=0;i<count;i++)
{
printf("%3d",arr[i]);
}


printf("\nPrime number array:");

for(t=0;t<n;t++)

printf("%3d",newarr[i]);

getchar();
getchar();
return(0);
}


in the question, it is asked to turn prime numbers to zero and then output it.. also it is asked to output prime numbers as another array.. could you please help me to complete this programme..
Was This Post Helpful? 0
  • +
  • -

#4 gecheme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 06

Re: C Question

Post icon  Posted 19 May 2006 - 12:16 PM

Amadeus, on 19 May, 2006 - 11:05 AM, said:

Hello,

The site has a policy by which we prefer to see some good faith effort on the part of the user before source code is provided in the matter of academic assignments. If you could please post the code you have written in an effort to solve the problem, our members would be more than happy to guide you.



3 minutes :) i have done the programme is above :)

This post has been edited by gecheme: 19 May 2006 - 12:17 PM

Was This Post Helpful? 0
  • +
  • -

#5 frog  Icon User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: C Question

Posted 19 May 2006 - 02:14 PM

ok i did not understand why are you declaring the size of the array as 9999.
i think the problem requires you to create an array with maximum 1000 elements.therfore the declaration should be:
int arr[1000];



now you use count as a variable to get the number of elements inn the array.can be done but i would also like to point out that this can be done by the means of dynamic array intialization.im not sure if you are familiar with 'new' operator.
int *arr=new int[count];




next

for (i=1;i<count;i++);
{
m=0;
for(j=2;j<=arr[i];j++)//loose the equal t sign .prime nos are divisible     by themselves
{
if(arr[i]%j==0)
m=1;
}

}//this brace should be at the end of the code

if(m==1)

arr[i]=arr[i];

else if(m==0)//this should involve a  compound statement
n++;//what is this???iit is not even initialized
newarr[i]=arr[i];//again parts of the array are left empty 

arr[i]=0;




i ahve commented the errors .i hope you get what am i reffering too.first the brace for the 'for' loop.you wan tthat the check should be performed on each and every element of the array.and hence action be taken.that is not happening.same thing for the else if loop .without braces you are just incrementing the value of n which stores some rubbish value as it is not intialized.

again u use
newarr[i]=ar[i];
that would leave empty spaces in the array.


i suggest that you should take a dry run i u want to check you progrm for run time errors.ull definetly find them
Was This Post Helpful? 0
  • +
  • -

#6 gecheme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 06

Re: C Question

Posted 20 May 2006 - 07:03 AM

i have to take an out put like that:

Enter an integer <1-1000>: 10
First array: 1 2 3 4 5 6 7 8 9 10
Second array: 1 0 0 4 0 6 0 8 9 10
Prime number array: 2 3 5 7

i wrote that programme:


#include <stdio.h>

int main (void)

{

int i, j, m, n, t, arr[1000], newarr[1000], count;

printf("Enter an integer <1-1000>");
scanf("%d",&count);

printf("First array:");
for(i=0; i<count; i++)
{
arr[i]=i+1;
printf("%3d",arr[i]);
}
for (i=1;i<count;i++);
{
m=0;
n=0;
for(j=2;j<arr[i];j++)
{
if(arr[i]%j==0)
{
m=1;
}

if(m==0)
{
newarr[n]=arr[i];
n++;
t=n;
arr[i]=0;
}

}

}
printf("\nSecond array:");

for(i=0;i<count;i++)
{
printf("%3d",arr[i]);
}

printf("\nPrime number array:");

for(n=0;n<t;n++)
{
printf("%3d",newarr[n]);
}
getchar();
getchar();
return(0);
}



but unfurtunately, i have a sample run as:

Enter an integer <1-1000>: 10
First array: 1 2 3 4 5 6 7 8 9 10
Second array: 1 2 3 4 5 6 7 8 9 10
Prime number array: 0

please help me.. do not say me do like this.. because my english is not good.. i can not understand.. if you run the programme and write down the true things i ll be happy.. i have been trying to do it for 3 days.. i m not good at c programming.. i only want to pass the course.. then i ll never see it anymore.. i only want the result..

This post has been edited by gecheme: 20 May 2006 - 07:05 AM

Was This Post Helpful? 0
  • +
  • -

#7 frog  Icon User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: C Question

Posted 20 May 2006 - 07:40 AM

i've commented the parts in the program where the changes need to be made. hope its clear enough :)

for (i=1;i<count;i++);//remove the semi colon after the loop.it works as an empty loop
{
m=0;
n=0;//remove this statement
for(j=2;j<arr[i];j++)
{
if(arr[i]%j==0)
{
m=1;
}//add a brace after this

if(m==0)
{
newarr[n]=arr[i];
n++;
t=n;
arr[i]=0;
}

}

}//remove brace




that should be correct(i compiled it this time. :)
Was This Post Helpful? 0
  • +
  • -

#8 gecheme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 06

Re: C Question

Posted 20 May 2006 - 09:48 AM

i m so stupid.. i m trying to find the mistake for 3 days.. and the mistake is a semi colon after the loop.. thank you for your help..

only error is that 2 is a prime number.. however, my programme says it is not a prime number :))
Was This Post Helpful? 0
  • +
  • -

#9 frog  Icon User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: C Question

Posted 20 May 2006 - 01:41 PM

View Postgecheme, on 20 May, 2006 - 09:10 PM, said:

i m so stupid.. i m trying to find the mistake for 3 days.. and the mistake is a semi colon after the loop.. thank you for your help..

only error is that 2 is a prime number.. however, my programme says it is not a prime number :))


you are welcome :)

i ran your program and the program takes 2 to be a prime number.

for(j=2;j<=arr[i];j++)



are you sure you removed the equal to sign as i to;d you earlier?????
Was This Post Helpful? 0
  • +
  • -

#10 gecheme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 06

Re: C Question

Posted 21 May 2006 - 10:12 AM

yes i removed it..

that is what i ve done..

#include <stdio.h>

int main (void)

{

int i, j, m, n, t, arr[1000], newarr[1000], count;

printf("Enter an integer <1-1000>");
scanf("%d",&count);

printf("First array:");
for(i=0; i<count; i++)
{
arr[i]=i+1;
printf("%3d",arr[i]);
}
n=0;
for (i=1;i<count;i++)
{
m=0;

for(j=2;j<arr[i];j++)
{
if(arr[i]%j==0)
{
m=1;
}

if(m==0)
{
newarr[n]=arr[i];
n++;
t=n;
arr[i]=0;
}

}

}
printf("\nSecond array:");

for(i=0;i<count;i++)
{
printf("%3d",arr[i]);
}

printf("\nPrime number array:");

for(n=0;n<t;n++)
{
printf("%3d",newarr[n]);
}
getchar();
getchar();
return(0);
}



the programme says 2 is not prime :blink:
Was This Post Helpful? 0
  • +
  • -

#11 frog  Icon User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: C Question

Posted 21 May 2006 - 08:09 PM

u need to add a brace over here.i've commented it in the code
for (i=1;i<count;i++);
{
m=0;
n=0;
for(j=2;j<arr[i];j++)
{
if(arr[i]%j==0)
{
m=1;
}
//add a brace here.removethe extra one



thats should do it.
Was This Post Helpful? 0
  • +
  • -

#12 gecheme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 06

Re: C Question

Posted 22 May 2006 - 11:15 AM

yessss:) thank you for your help :) it is working :)
Was This Post Helpful? 0
  • +
  • -

#13 frog  Icon User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: C Question

Posted 22 May 2006 - 11:21 AM

you are welcome :).
hope you have your assingnment completed in time :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1