Reputation: 49
Am trying to print out the values that are prime and not prime onto a 10x10 table. Arrays are confusing and cannot seem to get it, but I am assuming I need an array. The double XX on the expected output are just white space if the value is not prime.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j;
for (i = 2; i <= 101; i++)
{
for (j = 2; j <= i; j++)
{
if ( i % j == 0)
{
printf(" ");
break;
}
}
if( i == j)
{
printf("%d", i);
}
}
return 0;
}
Expected output:
02 03 XX 05 XX 07 XX XX XX 11
XX 13 XX XX XX 17 XX 19 XX XX
XX 23 XX XX XX XX XX 29 XX 31
XX XX XX XX XX 37 XX XX XX 41
XX 43 XX XX XX 47 XX XX XX XX
XX 53 XX XX XX XX XX 59 XX 61
XX XX XX XX XX 67 XX XX XX 71
XX 73 XX XX XX XX XX 79 XX XX
XX 83 XX XX XX XX XX 89 XX XX
XX XX XX XX XX 97 XX XX XX 101
Upvotes: 0
Views: 4155
Reputation: 153348
Divide and conquer: compute "is it a prime" and "print array" as separate problems.
The key is to break the task into manageable parts - architect the solution - and than implement the pieces. Use constants like I_MAX
rather than use a naked magic number like 101. With the print_array()
, think of how to print 1 element and wrap that into a loop of 10. Then wrap that loop into something that stops when all the numbers are printed.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define I_MIN 2
#define I_MAX 101
// OP all ready has code for IsPrime() - here is an alternative
bool IsPrime(int i, const bool *set) {
if (i <= 3) {
return i >= 2;
}
int divisor = 2;
for (;;) {
if (i % divisor == 0) {
return false;
}
if (i / divisor <= divisor) {
return true;
}
// get next prime
while (!set[++divisor])
;
}
}
void print_array(const bool *set, int min, int max) {
int width = 10;
for (int i = min; i <= max;) {
for (int w = 0; w < width; w++) {
// Print the number
if (set[i]) printf("%02d ", i);
else fputs("XX ", stdout);
i++;
if (i > max)
break;
}
fputc('\n', stdout);
}
}
int main(void) {
bool set[I_MAX + 1];
// Populate the set
for (int i = 0; i <= I_MAX; i++) {
set[i] = IsPrime(i, set);
}
// display the set
print_array(set, I_MIN, I_MAX);
return 0;
}
Upvotes: 2
Reputation: 8001
If it's just issues with formatting, it looks like you're missing the newline between rows. Before the end of your outer for loop add:
if((i -1) % 10 == 0) printf("\n");
This condition prints a newline every 10 iterations. The -1
is needed because the outer loop is started at 2.
Upvotes: 1
Reputation: 6190
What? No, no, no, you don't have to do all this complex stuff.
Just use a double loop: http://coliru.stacked-crooked.com/a/bc57bd52c29a419f
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j;
int my_array[10][10];
//Fill the array
for (i = 0; i < 10; ++i) {
for (j = 0; j < 10; ++j) {
my_array[i][j] = 10*i + j;
}
}
//Print out the array
for (i = 0; i < 10; ++i) {
for (j = 0; j < 10; ++j) {
printf("%d ", my_array[i][j]);
}
printf("\n");
}
return 0;
}
Make sure you get the indexing right. It should be relatively simple; never access the 10th index of an array of 10 elements; it's 0-9 and make sure that you print out the right value.
Upvotes: 3