0

Question at hand: Write a function primeTableInRange to generate a table to show whether each number in the range from startNum up to endNum is a prime number. When the number is not a prime number, we will only show a ‘*’. When the number is a prime number, we will show the number.

My code:

#include <iostream>
#include <ctime>
#include <cmath>
#include <cstdlib>


using namespace std;

int primetableinarray(int userarray[], int arraysize);

int main()
{
int startNum, endNum;
cout<< "Enter your first number in the range" << endl;
cin>>startNum;
cout<< "Enter your last number in the range" << endl;
cin>>endNum;

int arraysize = endNum - startNum;
int userarray[arraysize];
for (int i=startNum;i<=endNum;i++)
userarray[i]= startNum++;
primetableinarray(userarray, arraysize);
return 0;
}

int primetableinarray(int userarray[], int arraysize)
{

for (int i=2;i<arraysize;i++)
{
bool prime=true;
for (int r=2;r*r<i;r++)
{
    if (i % r ==0)
    {
        prime=false;
        break;
    }
}
if(prime) cout << i << endl;

else
    if(true) cout<< "*" << endl;
}
}

Issue is it doesn't start at "startNum" and doesn't end at "endNum". It actually goes from 0 to arraysize. Also it calculates 4 as a prime number. What am I missing here?

7 Answers 7

1

Be careful! Arrays always start at 0 and end at arraysize in your case. You cannot have arbitrary indexing. You could do the following:

int arraysize = endNum - startNum + 1;
int userarray[arraysize];
for (int i=0;i<arraysize;i++)
   userarray[i]= startNum+i;

Also, since we start at 0 you need to add +1 in ´arraysize´ to include ´endNum´ in your ´userarray´

Sign up to request clarification or add additional context in comments.

Comments

0

try to change this from:

for (int r=2;r*r<i;r++)

to

for (int r=2;r<i;r++)

Comments

0

At nowhere in your printing function do you even recognize your array. You simply begin looping numbers up to your array size. If you took the array out of your function arguments it would still work, so why are you including it? Your for loops just disregard any values in your array and begin looping from 2 arbitrarily.

As for why 4 is calculated as a prime number, it is because when your second loop starts it sees that 2*2=4 and therefore not less than 4, which is the number you are testing. This results in it skipping over the loop and never setting prime to false. Make the condition in the second for loop to <= or else any perfect square with no other factors will be labelled as prime, such as 25.

Also on a side note, how did this ever compile? You use a dynaimc variable to initiate an array size. That doesn't work and when I tried to run your code to see the output I got errors. Try using std::vector<int>. When you use the for loop to fill the vector you use the values as indexes which is completely and utterly wrong. This is when you should loop from zero to your arraysize because that it the address within the array. You also include unecessary headers like ctime and cmath, and have if(true) in your code for no reason.

Comments

0
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstdlib>


using namespace std;

int primetableinarray(int userarray[], int arraysize);

int main()
{
int startNum, endNum;
cout<< "Enter your first number in the range" << endl;
cin>>startNum;
cout<< "Enter your last number in the range" << endl;
cin>>endNum;

int arraysize = endNum - startNum  + 1;
int userarray[arraysize];
for (int i=startNum;i<endNum;i++)
userarray[i]= startNum++;
primetableinarray(userarray, arraysize);
return 0;
}

int primetableinarray(int userarray[], int arraysize)
{

for (int i=2;i<=arraysize;i++)
{
bool prime=true;
for (int r=2;r<i;r++)
{
    if (i % r ==0)
    {
        prime=false;
        break;
    }
}
if(prime) cout << i << endl;

else
    if(true) cout<< "*" << endl;
}
}

3 Comments

Please add some description what you are doing.
change " for (int r=2;r*r<i;r++) " to " for (int r=2;r<i;r++) "
change " int arraysize = endNum - startNum; " to "int arraysize = endNum - startNum + 1;"
0

The declaration for the array (int userarray[arraysize];) is illegal, the array bounds need to be known at compile time. This should not even compile, or it produces a zero-size array.
Afterwards, you randomly access unallocated memory, whcih is UB

Comments

0

Change

int arraysize = endNum - startNum + 1;
int userarray[arraysize];

To

int userarray[1];
int arraysize = endNum - startNum;
userarray[arraysize];

Also, add a return value to the primetableinarray function.

Comments

0

Here is the correct program .

#include <iostream>
using namespace std;
void  primetableinarray(int low, int high) ;
int main()
{
    int low, high, i, flag;
    cout<< "Enter low numbers  ";
    cin>> low;
    cout<< "Enter high numbers  ";
    cin>>high;
    
    cout<< "Prime numbers between " << low << "and are: " << high <<endl;;
    primetableinarray(low, high);    
    return 0;
}

void  primetableinarray(int low, int high) {

    int i, flag;
    while (low <= high)
    {
        flag = 0;

        for(i = 2; i <= low/2; ++i)
        {
            if(low % i == 0)
            {
                flag = 1;
                break;
            }
        }

        if (flag == 0)
            cout<< low <<endl;
        else 
		    cout<< "*" <<endl;
		    
        ++low;
    }

}

Output :
	
Enter low numbers  1                                                                                                                                 
Enter high numbers  10                                                                                                                               
Prime numbers between 1and are: 10                                                                                                                   
1                                                                                                                                                    
2                                                                                                                                                    
3                                                                                                                                                    
*                                                                                                                                                    
5                                                                                                                                                    
*                                                                                                                                                    
7                                                                                                                                                    
*                                                                                                                                                    
*                                                                                                                                                    
*

There are copule of problem in your code :

  1. int arraysize = endNum - startNum; int userarray[arraysize];

    How does it compile , it will be compilation error. you can allocate memory dynamically and use it .

  2. for (int i=startNum;i<=endNum;i++) userarray[i]= startNum++;

    this is wrong i = startNum and arraysize = arraysize +1 if you are comparing " i<=endNum " .

    Correct way is :

    for (int i=0;i<=endNum;i++) userarray[i]= startNum++;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.