Hi, I am trying to transfer some numbers that I have generated inside an array,but I have some mistake that I am not 'seeing'. The array contents are not the same as those generated. Any help please.. Thanks a lot.

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

#define SIZE 5

int main(void)
{
    int A[SIZE];
	
	int x;
	int i;
	
	srand( time( NULL ));
	
	
	for(i=0;i<5;i++)
	{
		x= 1+rand() % 6; // generating a random value
		A[i]=x;
		printf("%10d%10d",x,A[x]);	
		printf("\n");	
	}
	
	
	return 0;
}

printf("%10d%10d",x,A[x]);

Don't you mean A?

printf("%10d%10d",x,A[x]);

Don't you mean A?

There I am trying to display the random generators and the contents of the array next to each other... Is it incorrect to do it that way?

Thanks a lot

P.S I am still a beginner in C programming

There I am trying to display the random generators and the contents of the array next to each other... Is it incorrect to do it that way?

Thanks a lot

P.S I am still a beginner in C programming

x is your random number. So if x is 500 for example (I do realize that you've got it generating random numbers up to 6 so take this as as example) then you'll be trying to access A[500], but the array ends at A[4] so you'll get garbage output. What you want is to output from A[0] to A[4] so use the variable i and you'll find your program works just fine.

A[0] = random number 1
A[1] = random number 2, etc
while
A[random number] = ? (Who knows) ;)

x is your random number. So if x is 500 for example then you'll be trying to access A[500], but the array ends at A[4] so you'll get garbage output. What you want is to output from A[0] to A[4] so use the variable i and you'll find your program works just fine.

ahha yes of course!!, I see... thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.