C++ Project I've been stuck on for weeks...

I have been constantly trying to figure out how to make this code work. Well here is what I'm supposed to do.



Write a function named isPrime which accepts two integers say x and y.
As long as y > x, the program will print all primes between x and y inclusive.

Write a function named findPrime which accepts two integer parameters say a
and b. This function will print all prime numbers from a to b by calling the
function isPrime. Have your program cal this function using the user input
x and y.

Enhance the above step, so that your program will also print all perfect
square numbers between x and y inclusive. You may use the sqrt() function included in
<math.h> header file.

Example: If 3 and 30 are entered for x and y, your program should produce the
following output.

Prime: 3 5 7 11 13 17 19 29
(Prime count =9)

Square: 4 9 16 25
(Square count=4)

(Sum of all primes and squares: 181)


Okay, so I have been trying to figure out only how to do the Prime part on my own for so long, and I can't figure it out. I have to write functions, and I cannot do it any other way. I was never taught how to write a function, my instructor just threw this assignement at me.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Project 4 IsPrime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <math.h>

#define TRUE 1
#define FALSE 0


void main(void)
{    
	int x;
	int y;
	
	 
	

    printf ( "Please enter two positive integers-->");
     scanf ( "%d %d" , &x, &y );

        printf ( "\nPRIME: ");
		while ( scanf( "%d %d" , &x,&y)==2 )
		{ 
			findprime(x,y);
			printf ( "Enter two positive integers-->" );
			scanf ( "%d %d", &x, &y );
		}


   

}

void findprime( int a, int b) /* finds the prime number*/
{							/* checks to see if the numbers between x and y are prime */
	int p; //number of primes
	int i; //divisor
	

	
	{

		for (i=2; a<b; i++)
		{	if (isprime(i)) 
			
				printf ( "\t%d" , i)
			
		}
	}

		
			
}

	



int isprime(int k)
{	
	
	if ((a%i!==0)) // makes it prime?
			p++;
			return TRUE;
	else
		return FALSE;
}





If someone could help me out on this, it would be great! If someone could figure out a code for this so I can understand how each function is called, that would be beter! But anything is greatly appreciated!

Thank you,

-Adot2the
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream.h>
#include <cmath>

void findPrime(int, int);
int isPrime(int, int);

int main() {
  int x, y;
  cout << "Enter a value for x: ";
  cin >> x;
  cout << "Enter a value for y, such that y > x: ";
  cin >> y;
  cout << endl;
  findPrime(x, y);
}

void findPrime(int a, int b) {
  int sum=isPrime(a, b);
  bool fpsquare=false;
  int count=0;
  for (int i=a;i<=b;i++) {
    if ((int)sqrt(i)==sqrt(i)) {
      count++;
      sum+=i;
      if (!fpsquare) {
        fpsquare=true;
        cout << "Square: ";
      }
      cout << i << " ";
    }
  }
  if (count>0) cout << endl << "(Square count=" << count << ")" << endl << endl;
  if (sum>0) cout << "(Sum of all primes and squares: " << sum << ")" << endl;
}

int isPrime(int x, int y) {
  if (!(y>x)) return 0;
  bool prime;
  bool fprime=false;
  int count=0;
  int sum=0;
  for (int i=x;i<=y;i++) {
    prime=true;
    for (int j=2;j<i;j++) if (i%j==0) prime=false;
    if (prime) {
      count++;
      sum+=i;
      if (!fprime) {
        cout << "Prime: ";
        fprime=true;
      }
      cout << i << " ";
    }
  }
  if (count>0) cout << endl << "(Prime count=" << count << ")" << endl << endl;
  return sum;
}

Last edited on
Topic archived. No new replies allowed.