dimanche 27 octobre 2019

How can I reverse the output of my array when given an input of random numbers generated by the computer?

The issue I am currently having is that my code creates and prints 40 numbers into an array. When attempting to print this set of code backwards it will only print the final number in the regular/normal array when I need it to print the entire first array in reverse order. Thanks for the help. I am using c++.

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

int main(void) 
{ 
    int i;
    int lower = 0, upper = 100; //Random number between 1 and 100 is generated
    int rand_num; 
    int num[40];

    srand(time(0)); 

    printf("Your array is shown:\n");
    for(i = 0; i < 40; i++)
    {
        rand_num = (rand() % (upper - lower + 1)) + lower;
        num[rand_num] = rand_num;

        printf("%d ",num[rand_num]);

    }


    printf("\nYour array backwards is:\n");
    for(i = 39; i >= 0; i--)
    {
        printf("%d ", num[rand_num]);

    }


    return 0; 
} 



Aucun commentaire:

Enregistrer un commentaire