Array Help

Hi all,
I am a beginner to C++, so please excuse my basic questions.

I'm writing a program and it is going well, but I'm having trouble with this function. The instructions are to write a function that fills an array (that is passed in) with random numbers. There can be no duplicates, so I have to: generate the random number, check if it is in the array and, if it isn't, put it in the array. If it is then loop and generate a new number and test it. Could someone show and explain how to do this? I'm kinda stuck :/

Hi @Jacman11,
Can you post what
you have so far?
Hi @Jacman11.
Firstly test this instance and recognize. It set up table of numbers (matrix) m x n. Then generate random numbers, print it and calculate sum in column that user input.
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
#include "stdio.h"
#include <stdlib.h> // for access to "srand" and "rand"
#include <time.h>
#include "conio.h"
#include "iostream.h"

main()
{
int a[100][100];                                          // [row][column]
int i,j,n,m;
int sum;
srand(time(NULL));                                   // access to randomizer

  printf("Enter number of rows:\n");
  scanf("%i",&n);
  printf("Enter number of columns:\n");
  scanf("%i",&m);

for (i = 1; i <= n; i++)
	{for (j = 1; j <= m; j++)
{ a[i][j]=rand() % 10;
  printf(" %i ",a[i][j]);                                   // matrix printing 
}
  printf(" \n");
}

for (j=1; j<=m; j++)
{sum=0;
	 for (i=1; i<=n; i++)
{sum+=a[i][j];                                            // calculating sum in j-column
}
  printf("Summ of elements in %i",j);
  printf(" column: %i" ,sum);
  printf(" \n");
}

  printf("Press any key to close");

getch();
return 0;
}


Last edited on
Topic archived. No new replies allowed.