简体   繁体   中英

Array not printing?

I'm trying to print an array of 100 numbers in 10 rows and 10 columns, each number is randomly generated by a random number generator within a certain range. However, either nothing is printed or a single number is printed. How do I fix this?

I've looked at some examples of printing arrays but the code doesn't seem to work on my program. I've also looked at ways to seed random number generators with arrays however those as well don't seem to work.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main()
    {
        int myRandomNumber, i, a;
        srand(0);
        myRandomNumber = rand();
        double doubarr[10][10];
        for (a = 0; a <100; a++);
            for (i = 0; i<100; i++);
                doubarr[i][a]=(double)(rand()%100+1)/2.0;
        printf("%.2lf ", doubarr[i][a]);    
    }

I'd like the final results to print 10 rows and 10 columns of randomly generated numbers within the range specified in the code above. Thanks!

When you're learning C and using for always express it clearly with braces:

 for (a = 0; a <10; a++) {
   for (i = 0; i<10; i++) {
     doubarr[i][a]=(double)(rand()%100+1)/2.0;
     printf("%.2lf ", doubarr[i][a]);
   }
 }

You terminated each for loop early with ;so they individually did nothing.

Your code is effectively:

 for (a = 0; a <10; a++) { }
 for (i = 0; i<10; i++) { }

 doubarr[i][a]=(double)(rand()%100+1)/2.0;

Where this was using the last assigned value of a and actually stomped memory outside the array.

Another good habit to get into is to declare your iterators in the loop itself:

 for (int a = 0; a <100; a++) { ... }

printf("%.2lf ", doubarr[i][a]); This prints out one single value - specifically, the one at the location [i][a] in doubarr . Since there's no curly braces around your for loops, the print code is not executed until after they have both finished, since for loops with no braces only execute one single line after them. To print every value, you need to loop through every value in the array.

That being said, your code as written neither compiles nor runs, and will instead segfault: doubarr is size 10, 10, and you're trying to assign from size 0-100, 0-100.

Corrected code:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main()
    {
        int myRandomNumber, i, a;
        srand(0);
        myRandomNumber = rand();
        double doubarr[10][10];
        for (a = 0; a <10; a++) {
            for (i = 0; i<10; i++) {
                doubarr[i][a]=(double)(rand()%100+1)/2.0;
                printf("%.2lf ", doubarr[i][a]);
            }
        }    
    }

Demo

A single number is being printed since you only call printf() once and it is outside both for loops. You will have to move it inside the nested for loops if you want to print every value. Also, you array size is [10][10], but your loop goes to [100][100].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM