C++ numbers and arrays

I need this program to be able to read in a 3x3-element array of numbers. Have the user enter values into the array and then have the program choose one of them at random. Using the index of this element, make the program print all the pairs of prime numbers whose sum is the value of the specified value in the array.

So far I have got it to allow the user to enter the numbers and have go it separately generating random numbers, but putting it together has cause some errors in the code which I cant solve like not allowing me use more than one 'Int Main', I also need help in achieving the above.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
 #include "stdafx.h"
#include <random>
#include <chrono>
#include <iomanip>
#include <iostream>

const int MAXROWS{ 3 };
const int MAXCOLS{ 3 };

void InputValues(int num[][MAXCOLS]);
void DisplayValues(int num[][MAXCOLS]);
void FindNumbers(int num[][MAXCOLS]);
void displayArrayNumbers(int arr[][MAXCOLS]);
int randomChoice(int min, int max);



int main()
{
	int numbers[MAXROWS][MAXCOLS] = { 0, 0 };

	InputValues(numbers);
	DisplayValues(numbers);
	FindNumbers(numbers);


}

int main()
{
	int arrayNumbers[MAXROWS][MAXCOLS] = { { 2, 3, 4 }, { 23, 22, 55 }, { 653, 122, 245 } };
	int arrayElement{ 0 };

	displayArrayNumbers(arrayNumbers); // Call the function to initialized the array with random numbers.
	arrayElement = arrayNumbers[randomChoice(0, 2)][randomChoice(0, 2)];
	std::cout << "The random number from the array element is: " << arrayElement << std::endl;

	return 0;
}

void displayArrayNumbers(int arrrnd[][MAXCOLS])
{

	std::cout << "Displaying the " << MAXROWS << " x " << MAXCOLS << " array format\n";
	std::cout << "with random numbers...\n";
	for (int rows = 0; rows < MAXROWS; rows++)
	{
		for (int cols = 0; cols < MAXCOLS; cols++)
			std::cout << std::right << std::setw(5) << arrrnd[rows][cols];

		std::cout << std::endl;
	}
}


int randomChoice(int min, int max)
{
	unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
	std::default_random_engine generator(seed);
	std::uniform_int_distribution<int> distributionToss(min, max); // Set the numbers for int.
}





void InputValues(int num[][MAXCOLS])
{
	std::cout << "Enter the following integer numbers:\n";
	for (int rows = 0; rows < MAXROWS; rows++)
		for (int cols = 0; cols < MAXCOLS; cols++)
		{
			std::cout << "Row # " << rows + 1 << " Col #" << cols + 1 << " :";
			std::cin >> num[rows][cols];
		}
}

void DisplayValues(int num[][MAXCOLS])
{
	std::cout << "Displaying the numbers:\n";
	for (int rows = 0; rows < MAXROWS; rows++)
	{
		for (int cols = 0; cols < MAXCOLS; cols++)
		{
			std::cout << num[rows][cols] << "\t";
		}

		std::cout << std::endl;
	}
}

void FindNumbers(int num[][MAXCOLS])
{
	int findNum{ 0 };
	bool found{ false };

	std::cout << "Find a number in the " << MAXROWS << " x " << MAXCOLS << std::endl;
	std::cout << "Input an integer number: ";
	std::cin >> findNum;

	for (int rows = 0; rows < MAXROWS; rows++)
		for (int cols = 0; cols < MAXCOLS; cols++)
		{
			if (num[rows][cols] == findNum)
			{
				std::cout << "Found it on Row #" << rows + 1 << " and Col #" << cols + 1 << std::endl;
				found = true;


			}

		}

	if (found == false)
		std::cout << "The number " << findNum << " was not found.\n";





}
Why do you want more than one main function? What are you trying to achieve by that?
displayArrayNumbers() and DisplayValues() do basically the same thing.

Remove one of them. Lines 59 and 60 declare two functions, they don't call them.

I don't think you need FindNumbers(). The problem as described says that the program should pick the number.
how do you declare the functions
You're already declaring your functions, at lines 10 - 14.

EDIT: You seem to have ignored Moschops' question:

Moschops wrote:
Why do you want more than one main function? What are you trying to achieve by that?
Last edited on
Topic archived. No new replies allowed.