for loop exits with out iterating

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).

No idea what "bailed on" means.
The code is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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:


2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 49 53 59 61 67 71 73 79 83 89 91 97 101


which are nearly all correct apart from 49 and 91.

Hello Spamfilter,

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.

Andy
To obtain prime numbers (which is what I think you're trying to do), then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <numeric>

constexpr unsigned 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' :  ' ');

}



2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101

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:

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
#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(const int* 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; 
}
You should google "sieve of Eratosthenes". In c++ it's pretty quick and compact in memory usage if you use vector<bool> as a container type.
@spamfilter - see my 'fixed' code above re prime numbers. There is a problem with your maskList().

Topic archived. No new replies allowed.