When the maskList() function is called in the "if" statement, the for loop gets bailed on, instead of continuing to iterate like it is supposed to. I can't figure out what I am doing wrong.
1 2 3 4
for (int i = 2; i < 100; i++) {
if (list[i] != 0)
{ maskList(i); }
}
if I leave off the "if" statement, then it iterates properly, but then maskList() is called 98 times instead of just 25
1 2 3 4 5
void maskList(int x) {
for (int li = x; li < 100; li++) {
if (list[li] % x == 0) { list[li] = 0; }
}
}
@Spamfilter, please post complete code, not little snippets like this. Also, supply the input and an explanation of what you are trying to do (no, it's not obvious).
#include <iostream>
#include <numeric>
int List[100] {};
void maskList(int x) {
for (int li = x; li < 100; li++)
if (List[li] % x == 0)
List[li] = 0;
}
int main() {
std::iota(List, List + 100, 2);
for (int i = 2; i < 100; i++)
if (List[i] != 0)
maskList(i);
for (size_t i = 0; i < 100; ++i)
if (List [i])
std::cout << List[i] << ' ';
}
It looks like you're doing something to generate prime numbers. The above code displays:
As lastchance mentioned it is best to provide the complete code that can be compiled and tested or at least enough to demonstrate the problem.
No one has any idea how "list" is defined or if comparing the elements to an "int" (0)zero will work.
I can see nothing wrong with the code posted except that the if statement may evaluate to false for each iteration. That is something that can be checked with the debugger.
#include <iostream>
#include <numeric>
constexprunsigned arrsze {100};
unsigned List[arrsze] {};
void maskList(unsigned x) {
for (unsigned li {x + 1}, div {List[x]}; li < arrsze; ++li)
if (List[li] % div == 0)
List[li] = 0;
}
int main() {
std::iota(List, List + arrsze, 2);
for (unsigned i = 0; i < arrsze; ++i)
if (List[i])
maskList(i);
for (unsigned i = 0; i < arrsze; ++i)
if (List[i])
std::cout << List[i] << (i == arrsze - 1 ? '\n' : ' ');
}
As was surmised, I am trying to create a list of prime numbers from 1-100. in the 'for' loop that calls 'maskList()' it works for i=2, but then it bails out of (exits) the loop so only the even numbers after 2 are masked (changed to 0). If I comment out the 'if' statement, then i is iterated all the way through 100 and I am left with just the 25 prime numbers.
What I am trying to do with the 'if' statement is to skip over all the instances where a value was already set to '0' by maskList() so that it is only called when necessary (there is no reason to call maskList() for 1=3, because the value of List[3] was already masked) rather than every time. here is the completed code:
#include <cstdio>
// initializing arrays for the 1st 100 numbers and the first 25 Prime numbers
int list[100]; // list of the numbers 1-100
int Prime[25]; // list of the 1st 25 prime numbers
//seed list with the 1st 100 numbers
void seedList() {
for (int i = 0; i < 100; ++i) {
list[i] = i + 1;
};
}
//create mask to eliminate all non-prime numbers from list
void maskList(int x) {
for (int li = x; li < 100; li++) {
if (list[li] % x == 0) { list[li] = 0; }
}
}
// copy prime numbers into Prime[] array
void getPrimes(constint* cPrimes) {
int pI = 0;
for (int i = 0; i < 100; i++) {
if (cPrimes[i] != 0) {
Prime[pI] = cPrimes[i];
// printf(" %d", Prime[pI]);
pI++;
}
}
}
// print the seed list
void printList() {
for (int i = 0; i < 100; i++) {
printf(" %d", list[i]);
}
}
void printPrime() {
for (int i = 0; i < 25; i++) {
printf(" %d", Prime[i]);
}
}
int main() {
seedList();
printList();
puts("\n");
for (int i = 2; i < 100; i++) {
if (list[i] != 0)
{ maskList(i); }
}
// for (int i = 2; i < 100; i++) {
// maskList(i);
// }
list[0] = 0;
countPrimes(list);
getPrimes(list);
printPrime();
return = 0;
}