random numbers

hello I am trying to make a program which takes an array of ints as an argument and fills the array with random values in the range of 0 to 1000 but all that's happening is its turning units in to tens for example if I enter 3 it will become 13, if I enter 13 it will become 113 and I am really struggling to find an answer if anybody could be of any help I would be grateful.

other information: it has to have the prototype:
void fillarray( int data[], int n);

that's all.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// Date
#include <iostream>
using namespace std;  //Tells the compiler that your using the standard namespace
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//function prototypes
void fillarray( int data[], int n);
void display(int data[], int n);
int main()
{
	int data1[1]={};
	fillarray(data1,1);
	display(data1,1);
	
	system ("pause");
    return 0;   // indicate that program ended successfully
}

void fillarray (int data[], int n)
{	
	int i;

	for (i=0;i<n;i++)
	{
		cout << "enter value for item "<< (i+1) <<": ";
		cin >> data[i];
		data[i];
	}	
}

void display(int data[],int n)
{
	
	for (int i=0;i<n;i++)
	{
		cout<<"the value of item" << " is "<< (i+1) << data[i] << endl;
	}
return ;
}

Last edited on
The "problem" is on line 39:

cout<<"the value of item" << " is "<< (i+1) << data[i] << endl;

If for example i is 32 and data[i] is 17, the line above will print:

the value of item is 3317


You forget to put a space between the values you print.
And you also print i+1 for what reason exactly? You probably intended this:

cout<<"the value of item" << " is " << data[i] << endl;
thank you It worked but how would you take an array of ints as an argument and fill the array with random values between 0 and 1000

Last edited on
how would you take an array of ints as an argument and fill the array with random values between 0 and 1000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

    using namespace std;

void fill(int a[], int s);
void print(int a[], int s);

int main()
{
    srand(time(0));             // seed random number generator
    
    const int size = 5;
    int array[size] = { 0 };    // initial array of zeros
    
    print(array, size);

    fill(array, size);          // fill with random numbers

    print(array, size);

}

void fill(int a[], int s)
{
    for (int i=0; i<s; i++)
        a[i] = rand() % 1000; // generate value in range 0 to 999 inclusive
}

void print(int a[], int size)
{
    for (int i=0; i<size; i++)
        cout << setw(4) << a[i];
    cout << endl;
}

Output:
   0   0   0   0   0
 445 310 401 366 105
thank you this is exactly what I needed this has been very helpful thank you
Topic archived. No new replies allowed.