4 Replies - 2043 Views - Last Post: 25 October 2007 - 11:36 AM Rate Topic: -----

#1 garc2541   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-October 07

Printing "*" for prime numbers between 5 and 49

Post icon  Posted 23 October 2007 - 08:52 PM

I'm a freshmen in college and I'm taking an intoductory c++ course. My assignment asked me to print out all odd numbers from 5 to 49 and to then write a couple of functions. Anyway it asks for a function that will print out a " * " if the number is prime. I did that and a " * " prints our for all the numbers which is false because some of the numbers aren't prime. What did I do wrong and I will appreciate any hints. Also how do u calculate an avg in c++? Thank You


 #include <iostream>
using namespace std;



int sumsq(int);
int sumcb(int);
int prime(int);


int main()
{
cout<<" Number SumSq   SumCb   PRIME "<<endl;
cout<<endl;

	
int numb;
int sum;
int sum1;




for(numb=5;numb<=49;numb+=2)
   if(prime(numb)==1)
   
cout<<numb<<"	   "<<sumsq(numb)<<"	  "<<sumcb(numb)<<endl;



sum=sumsq(numb);
sum1=sumcb(numb);







system ("pause");
return 0;
}

int sumsq(int numb){
int yoyo;
int sum =0;

for(yoyo=1;yoyo<=numb;yoyo++)
sum+=yoyo*yoyo;
return sum;
}

int sumcb(int numb){
int val;
int sum1 = 0;

for(val=1;val<=numb;val++)
sum1+=val*val*val;
return sum1;
}   


int prime(int numb){
int p;

  for(p=2;p<=numb;p++)
  if(numb%1==0){
  cout<<"					"<<"*"<<endl;
  return 1;
  }
  else{
  return 0;
  }
}	   



Is This A Good Question/Topic? 0
  • +

Replies To: Printing "*" for prime numbers between 5 and 49

#2 jjhaag   User is offline

  • me editor am smartastic
  • member icon

Reputation: 49
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Printing "*" for prime numbers between 5 and 49

Posted 23 October 2007 - 09:41 PM

One of the problems with your prime() function is that any value modulus one is equal to zero. Perhaps you meant to take the number modulo p instead?

Also, since you have a return statement in both of the alternatives of your if-else structure in that function, the loop will not work - it will return a value and exit the function on the first round through the loop. THe following may work a little better:
int prime(int numb) {
	int p;

	for (p=2; p<=numb/2; p++) {
		if (numb%p==0) {
			return 0;
		}
	}
	cout << "*" << endl;
	return 1;
}
}

Once the loop is entered, the if statement tests whether the number is evenly divisible by p. If it is, the number is not prime, and the function returns a zero and exits. If it is not, the loop continues. If the loop makes it all the way through to the end without finding a factor, the number is prime, so it outputs a * and returns one.

Notice that I have also enclosed the entire contents of the for loop in { }, and only run the loop up to numb/2 - you don't need to go all the way to numb, because any number greater than numb/2 cannot be a factor (since the corresponding factor would be less than 2).

I haven't looked over the rest of it in great detail, but that should at least give you a start.

Hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

#3 garc2541   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-October 07

Re: Printing "*" for prime numbers between 5 and 49

Posted 24 October 2007 - 05:14 PM

This code works I tried it, but I need to print out all the odd numbers from 5 to 49 and then if the number is prime a star is suppose to come up. In your code only the prime numbers come out. Thanks alot I really appreciate the help. Ill try to see if I can fix it. :^:



EDIT: I dont know how to make the prime function print out all the numbers and only put a star where there is a prime number. Can some one please help me?

This post has been edited by garc2541: 24 October 2007 - 08:41 PM

Was This Post Helpful? 0
  • +
  • -

#4 Amadeus   User is offline

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

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Printing "*" for prime numbers between 5 and 49

Posted 25 October 2007 - 05:39 AM

The prime function is not meant to do all the testing and printing...only to decide is a number is prime or not. You need to integrate it into the rest of your code, like so (pseudocode):
if prime(mynum) AND mynum is ODD and mynum is greater than 4 AND mynum is less than 50 then
   print *
end if


Was This Post Helpful? 0
  • +
  • -

#5 Smarf   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 80
  • Joined: 21-September 07

Re: Printing "*" for prime numbers between 5 and 49

Posted 25 October 2007 - 11:36 AM

View Postgarc2541, on 24 Oct, 2007 - 05:14 PM, said:

This code works I tried it, but I need to print out all the odd numbers from 5 to 49 and then if the number is prime a star is suppose to come up. In your code only the prime numbers come out. Thanks alot I really appreciate the help. Ill try to see if I can fix it. :^:



EDIT: I dont know how to make the prime function print out all the numbers and only put a star where there is a prime number. Can some one please help me?


- You need a loop to run through all the numbers 5 - 49

- You need to run the prime function above on each number

- Inside your loop have an if/else statement that basically says
If (numIsPrime)
{
   cout << " * ";
}
Else
{
   cout << number;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1