I am trying to get my program to work and I think I got it pretty good the only part that I am having issues with is getting the array to display the correct prime numbers. I have ran the program and it seems to find all the correct prime numbers. Here is the instructions:
Display a title, then ask the user to specify a range of positive integers with a low and high boundary. For each entry, repeat until the user enters a positive number. After a positive high boundary has been entered, test for low boundary less than or equal to the high boundary and if it is not repeat the process. After you find the prime numbers display them with the boundaries.
-1 means not prime (crossed out)
+1 means prime (circled)
0 means unknown
Five required functions:
(a) Given an integer size, return an array of integers of that size, with all elements initialized to zero, except for elements at indexes 0 and 1, that are set to 'Not prime' (-1) (use the 'return' statement to return the new array)
(b) Given an array of integers, and the size of that array, return the index of the first element that contains a zero. (Use the 'return' statement)
(c) Given an array of integers, the size of that array, and an integer factor, 'circle' the element at the index equal to the given factor (store +1 there), then scan the rest of the array and 'cross out' (mark with -1) every other element with an index that is a multiple of the given factor.
(d) Given an array of integers, and the size of that array, scan the array and convert every element that contains a zero (unknown) to a +1 (circled).
(e) Given an array of integers, and the size of that array, return another array of integers that contains the indexes of the elements in the first array that are 'circled' (contains +1), and also return the size of that second array. (Use the 'return' statement to return the new array, and use the reference parameter to return the array's size).
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#include <iostream>
#include <math.h>
using namespace std;
int *initializeArray(int size);
int zeroElement(int tempArray[], int size);
void factorArray(int tempArray[], int size, int factor);
void scanArray(int tempArray[], int size);
int *loadNewArray(int tempArray[], int size, int &count);
int main()
{
int low_Bound, high_Bound, first_zero, count = 0;
int *mainArray, *secondArray;
char again;
cout << "The Sieve of Eratosthenes" << endl << endl;
do
{
do
{
do
{
cout << "Enter the high boundary for the range of positive integers: " << endl;
cin >> high_Bound;
if (high_Bound <= 0)
{
cout << "High bound must be positive" << endl;
}
} while (high_Bound <= 0);
do
{
cout << "Enter the low boundary for the range of positive integers: " << endl;
cin >> low_Bound;
if (low_Bound <= 0)
{
cout << "Low boundary must be positive" << endl;
}
} while (low_Bound <= 0);
if (low_Bound > high_Bound)
{
cout << "The low boundary must be smaller than the high boundary" << endl;
}
} while (low_Bound > high_Bound);
mainArray = initializeArray(high_Bound);
first_zero = zeroElement(mainArray, high_Bound);
factorArray(mainArray, high_Bound, first_zero);
scanArray(mainArray, high_Bound);
secondArray = loadNewArray(mainArray, high_Bound, count);
cout << "The prime numbers in your range is: " << endl;
for (int i = 0; i <= count; i++)
{
cout << secondArray[i] << endl;
}
cout << "Would you like to test another range of integers (y/n): " << endl;
cin >> again;
} while (again != 'n');
return 0;
}
// (a)
int *initializeArray(int size)
{
int *tempArray = new int[size];
for (int i = 0; i < size; i++)
{
if (i == 0)
{
tempArray[i] = -1;
}
else if (i == 1)
{
tempArray[i] = -1;
}
else
{
tempArray[i] = 0;
}
}
return tempArray;
}
// (b)
int zeroElement(int tempArray[], int size)
{
int zero_position = -1, i = 0;
do
{
if (tempArray[i] == 0)
{
zero_position = i;
}
i++;
} while (zero_position == -1);
return zero_position;
}
// (c)
void factorArray(int tempArray[], int size, int factor)
{
tempArray[factor] = 1;
for (int i = factor * 2; i < size; i += factor)
{
tempArray[i] = -1;
}
}
// (d)
void scanArray(int tempArray[], int size)
{
for (int i = 0; i < size; i++)
{
if (tempArray[i] == 0)
{
tempArray[i] = 1;
}
}
}
// (e)
int *loadNewArray(int tempArray[], int size, int &count)
{
int j = 0;
count = 0;
for (int i = 0; i < size; i++)
{
if (tempArray[i] == 1)
{
count++;
}
}
int *temp = new int[count];
for (int i = 0; i < size; i++)
{
if (tempArray[i] == 1)
{
temp[j] = i;
j++;
}
}
return temp;
}
|
Also if (c) is running correctly I am getting returns that look like first run:
9594898468
-486918638
3
418994893
5
4896489341
second run:
9594898468
-486918638
3
418994893
5
4896489341
7
-189948364
9
-848948348
11
third run:
9594898468
-486918638
3
418994893
5
4896489341
7
-189948364
9
-848948348
11
-489483848
13
9594898468
-486918638
9594898468
-486918638
8486483488
And this run is with a high boundary of 15 and a low boundary of 1. This makes me think that the (e) function is not working correctly by returning the array of ints back correctly
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
// (e)
int *loadNewArray(int tempArray[], int size, int &count)
{
for (int i = 0; i <= size; i++)
{
if (tempArray[i] == 1)
{
count++;
}
}
int *temp = new int[count];
for (int i = 0; i <= count; i++)
{
if (tempArray[i] == 1)
{
temp[i] = i;
}
}
return temp;
}
|
These are the only two areas that I am having trouble with any help would be awesome.. Thanks in advance