Homework Help Please OSU C++

Hello i am having an issue with the code i am posting below. The code is for an out lab homework assignment for Ohio state university CSE1222. The issue i am having is with my second function call at the bottom of the code. The program is supposed to read in a min and max number, check certain parameters and then do a for loop to print out the prime numbers from the min to the max number provided. The program has two different function calls, one to check for an error which works and another to check each number in the for loop to ensure it is a prime. The function call bool is_prime is the function i am unable to work properly. It is supposed to only return a true or false value but i am not sure what part of my boolean expression is incorrect. When the program runs it checks for the error correctly and then says floating exception. Any hints or obvious errors someone can point out will be greatly appreciated. I will continue to work on it and hopefully solve this issue. Thanks for any help provided!!

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
/*
  File: isprime.cpp
  Created by: James Selhorst
  Creation Date: 6/14/13
  Synopsis: ??
  
*/

#include <iostream>
#include <cmath>

using namespace std;

// FUNCTION PROTOTYPE FOR read_range

double read_range(int &imin, int &imax);

// FUNCTION PROTOTYPE FOR is_prime

bool is_prime(int prime);

// DO NOT MODIFY THE MAIN ROUTINE IN ANY WAY
int main()
{
  int imin(0), imax(0);

  // Read in range
  read_range(imin, imax);

  // Print prime numbers
  cout << "Primes:";
  for (int j = imin; j <= imax; j++) {
    if (is_prime(j)) 
      { 
        cout << "  " << j; 
      }
  }
  cout << endl;

  return 0;
}

// DEFINE FUNCTION read_range() HERE:
double read_range(int &imin,int &imax)
{
  cout<<"Enter minimum and maximum :";
  cin>>imin>>imax;
  
  while(imin<2 || imax<2)
    {
      cout<<"Error. Minimum and maximum must be at least 2"<<endl;
      cout<<"Enter minimum and maximim :";
      cin>>imin>>imax;
    }
  
}

// DEFINE FUNCTION is_prime() HERE:


bool is_prime(int prime)
{
  int imax,j;
  if(j%imax!=0)
    {prime=true;}
  
}
1
2
3
4
5
6
7
bool is_prime(int prime)
{
  int imax,j;
  if(j%imax!=0)
    {prime=true;}
  
}
¿what values do 'j' and 'imax' hold?
Imax is a number entered by the user and that function is in the read_range call function. The J is an arbitrary number in a for loop in the main function. It starts at imin (read_range) and goes to imax. The goal is that each time j changes from imin to imax it puts that number into is_prime and computes if it is a prime number. My instructor said the bool statement is really easy and is only three lines of code. I have tried to use call by value and do bool is_prime(int j, int imax) and then make prime a variable inside the function just to compute if it is a prime number. However whenever the function is_prime(j) is used it only inputs one parameter of j. So im not sure how to include imax in the function when it is user input in a different call function.
You never actually assign a vaue to imax on line 63 and that is a veryyyyyyy slow method at finding primes.
Compare these
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
void fast( int min , const int &max , std::vector<int> &primes )
{
	int i( 3 );
	bool prime( true );
	if( min == 1 || min == 2 ) primes.push_back( 2 );
	if( min % 2 == 0 ) ++min;
	if( min == 1 ) min += 2;

	while( min < max )
	{
	    if( min > max ) min == max;
		prime = true;
		for( const auto &it : primes )
		{
			if( min % it == 0 )
			{
				prime = false;
				break;
			}
		}
		if( prime )
		{
			primes.empty() ? i = 3 : i = primes.back() + 2;
			for( ; i < min/2; i += 2 )
            {
                if( min % i == 0 )
                {
                    prime = false;
                    break;
                }
            }
			if( prime ) primes.push_back( min );
		}
		min += 2;
	}
}

void slow( int min , int max , std::vector<int> &primes )
{
    bool prime( true );
	if( min == 2 || min == 1 ) primes.push_back( 2 );
	if( min == 1 ) min += 2;
	if( min % 2 == 0 ) ++min;
	for( ; min < max; min += 2 )
	{
	    prime = true;
	    if( min > max ) min = max;
		for( int i( 3 ); i < min / 2; i += 2 )
		{
			if( min % i == 0 )
            {
                prime = false;
                break;
            }
		}
		if( prime ) primes.push_back( min );
	}
}


You are doing the slow method AFAIK
i am only in a intro to c++ class lol all we know is the slow method
You don't know common logic that you should try and see if it is divisible by the other primes then go up by 2 each time? Since all composites are made up of primes so if it is divisible by any of the previous primes then it is not a prime and all primes are odd except for 2 so you can increase by two each time.
My code is really messy also because I can't be bothered to make it look neat =p and wrote it up pretty fast so might be a few bugs

*edit also idk why the indentation is all messed up when I posted. It looks like normal indentation in codeblocks and all I did was copy/paste my code.

**edit after looking at the earlier post as I was typing. I Ne55 asked the same thing. about that function on line 60. It seems to me like you are tyring to use imax and j as "global" variables but they are not. Maybe try and pass them as parameters to your function?
Last edited on
I figured out what i was doing wrong i just created a for loop inside the bool is_prime function to determine all number from min to max. The reason i didnt create code like yours you may ask yourself is perhaps because it is a intro to c++ class. I do know the logic behind how to determine a prime number, however not how to use code that we have not learned yet. So posting that entire code was a huge waste of yours and my time

out
Logic behind a prime number is not c++ it is math.
There are several ways. THe most efficient wya is to use Sieve. Which states this:
To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:
Create a list of consecutive integers from 2 to n: (2, 3, 4, ..., n).
Initially, let p equal 2, the first prime number.
Starting from p, count up in increments of p and mark each of these numbers greater than p itself in the list. These will be multiples of p: 2p, 3p, 4p, etc.; note that some of them may have already been marked.
Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this number (which is the next prime), and repeat from step 3.

which would look something liek this
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
#include <iostream>
#include <vector>

int main()
{
    int min( 0 ) , max( 0 );
    std::cout << "min: " << std::flush;
    std::cin >> min;
    std::cout << "max: " << std::flush;
    std::cin >> max;
	std::vector<int> primes( 0 );
	for( int i( 2 ); i < max; ++i )
	{
		primes.push_back( i );
	}

	auto it( primes.begin() );

	while( it != primes.end() )
    {
        for( auto it2( it + 1); it2 != primes.end(); ++it2 )
        {
            if( *it2 % *it == 0 )
            {
                primes.erase( it2 );
                --it2;
            }
        }
        ++it;
    }

	for( const auto &it : primes )
    {
        if( it >= min ) std::cout << it << std::endl;
    }
}

Which runs extremely fast especially if you put in the min/max yourself with out using ifstream for that.
hello devildeuces.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool is_prime(int prime)
{
    int r;
       for (int k= 2; k< prime; k++)
        {
            r = prime % k;
            if (r == 0)
            return 0;
            
            
        }
    return 1;
}

bye
Last edited on
Topic archived. No new replies allowed.