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;
}

Dani AI

Generated

The surprising output comes from using the generated value as an array index instead of using the loop index. As pointed out, that treats the random number as a subscript and can access memory outside A[0..SIZE-1], which produces undefined behavior and "garbage" results. The values you store live at A[i]; read them back with the same index.

A minimal corrected pattern (only the loop shown) looks like this:

for (i = 0; i < SIZE; ++i) {
    int x = 1 + rand() % 6;
    A[i] = x;
    printf("%10d %10d\n", x, A[i]);
}

Practical tips and cautions:

  • Use the SIZE macro in the loop condition rather than hard-coding “5”, so the loop and array size stay consistent.
  • Always include a newline in printf for readable output.
  • Accessing A[x] where x can be 5 or 6 (from 1 + rand()%6) will read past the end of the array and invoke undefined behavior; never index outside 0..SIZE-1.
  • Compile with warnings enabled (-Wall -Wextra) and, when available, run with AddressSanitizer (-fsanitize=address,undefined) or valgrind to catch out-of-bounds accesses.
  • If moving to modern C++, prefer the <random> facilities instead of rand() for better-quality and safer random numbers.

This explains why the stored numbers didn’t match the printed values: the program was reading unrelated memory. The corrected loop keeps storage and retrieval aligned and avoids the out-of-range access that caused the problem.

Recommended Answers

All 4 Replies

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.