Array Assistance

+2 Lauren N · November 2, 2015
Hey everyone. I am new to programming and I am beginning to struggle with arrays. One of the problems involves the Sieve of Eratosthenes algorithm, where I must prompt the user for a number, find and print all the numbers less than or equal to that number put in by the user, all using arrays. I have no idea how to include arrays in this problem. Any advice?

Post a Reply

Replies

Oldest  Newest  Rating
+1 Jon Z. · November 2, 2015
#include <stdio.h>



main(){

    int i;

    int num = 0;

    printf("Enter number: ");

    scanf(" %d", &num);

    int array[num];


    for(i=0; i<num; i++){

        array = i+1;

        printf("%d\n", array);

    }


    return 0;

}
0 Lauren N · November 2, 2015
Thank you!!!
0 Lauren N · November 2, 2015
I tried this, and it said int [num] is not assignable..
0 Akash Singh · November 2, 2015
array = i ;
0 Akash Singh · November 2, 2015
after for loop , initialize it as 
array = i+1 ;
print it as array ;
+1 Jon Z. · November 3, 2015
if the previous code didn't work you can try this.


#include <stdio.h>

main(){
    int i;
    int num = 0;
    int * array;

    printf("Enter number: ");
    scanf(" %d", &num);

    array = (int*) malloc(num * sizeof(int));

    for(i=0; i<num; i++){
        array = i+1;
        printf("%d\n", array);
    }

    free(array);

    return 0;
}
  • 1

C

106,984 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator