Need this programmer to display the correct numbers and select a random number.

Apr 7, 2016 at 12:01pm
Need this program to be able display the numbers the user enters in 3 x 3 then be able to pick a random number in the 3x3 matrix.

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
 #include "stdafx.h"

#include <iostream>
using namespace std;

int matrix[3][3];

int main()
{

	const int Numb = 9;
	int a[Numb]; //10 elements
	cout << "Enter 10 values:"; //prompts user for 10 values.
	for (int i = 0; i < 9; i++)
	{
		cout << "\nEnter value: ";
		cin >> a[i]; // puts values in array
	}

	for (int x = 0; x<3; x++)
	{
		for (int y = 0; y<3; y++)
		{
			matrix[x][y] = 1;
		}
	}



	for (int x = 0; x<3; x++)  // loop 3 times for three lines
	{
		for (int y = 0; y<3; y++)  // loop for the three elements on the line
		{
			cout << matrix[x][y];  // display the current element out of the array
		}
		cout << endl;  // when the inner loop is done, go to a new line
	}



	return 0;

}
Apr 7, 2016 at 12:19pm
so what exactly is the problem?

your'e asking for 10 values, but you are only getting 9 and you only need 9.
Apr 7, 2016 at 4:13pm
Just a quick idea:

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
#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]);

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

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


    return 0;
}

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

}
Last edited on Apr 7, 2016 at 4:53pm
Apr 8, 2016 at 10:28am
I want the program to choose one of the numbers at random at random rather than the user doing it, how could I expand from this and make the program print all the pairs of prime numbers whose sum is the value of the specified value in the array.
Apr 8, 2016 at 1:55pm
You can generate random numbers with rand() (stdlib.h)
matrix[rand() % 3][rand() % 3]

To get the pairs of prime numbers you could do something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for(int x = number; number > 0;)
{
	if(isPrime(x)) //check if x is prime
	{
		if(x > number)
		{
			x--;
		}
		else
		{
			number = number - x;
			//output or whatever
		}
	}
	else
	{
		x--;
	}
}
Apr 11, 2016 at 11:39am
Could you put it together with the code because I keep getting errors where ever I place.
Apr 11, 2016 at 12:38pm
What errors? Where are you trying to place it?
Apr 12, 2016 at 1:04pm
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.
Apr 12, 2016 at 2:34pm
You are saying, "I keep getting errors where ever I place." Where is the updated code? What are you having issues with? Is it the arrays? Is it the random function? Is it the prime numbers? You have plenty of information to work on your code. Post the updated code and be specific on what issues are you having.
Apr 12, 2016 at 3:33pm
BTW, considering the original post, all you have is a 3x3 array filled with 1. You haven't added any user input to the matrix.
Also, your input array has 9 elements, and you d read 9 elements, but your comment and prompt say 10 elements.
Apr 12, 2016 at 5:15pm
Is the program supposed to look something like this?
Enter the following integer numbers:
Row # 1 Col #1 :1
Row # 1 Col #2 :2
Row # 1 Col #3 :3
Row # 2 Col #1 :4
Row # 2 Col #2 :5
Row # 2 Col #3 :6
Row # 3 Col #1 :7
Row # 3 Col #2 :8
Row # 3 Col #3 :9
Displaying the numbers:
1	2	3	
4	5	6	
7	8	9	
The random number from the array element is: 9
Found it on Row #3 and Col #3
The number 9 is prime.
Pair: 2 + 7


To give you an idea on how to randomly select an array element:
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
#include <iostream>
#include <random>
#include <chrono>
#include <iomanip>

// Global constant to setup the array size across main and other functions.
const int MAXROWS{ 3 };
const int MAXCOLS{ 3 };

//Function Prototype
void displayArrayNumbers(int arr[][MAXCOLS]);
int randomChoice(int min, int max);

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.

    return distributionToss(generator);

}
Last edited on Apr 12, 2016 at 5:18pm
Apr 13, 2016 at 10:39am
That's exactly how I want the program to work, how do I get it to function like that.
Apr 13, 2016 at 10:49am
You look seriously at the code chicofeo posted, understand it, learn the techniques s/he's using, and then use the same techniques to achieve what you want in your code.

EDIT: And if there are specific things you don't understand, you ask specific questions about those specific things, so that we can give you specific answers.
Last edited on Apr 13, 2016 at 10:50am
Apr 13, 2016 at 10:58am
This is as far as I have got putting the code together but I still get a lot of errors.

#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";





}
Apr 13, 2016 at 11:06am
1) Please use code tags when posting code, to make it readable.

2) What errors? What do you hope to achieve by withholding information from us? How do you expect us to help you if you won't give use the useful information that you have?
Apr 13, 2016 at 11:16am
I've managed to get it working just don't know how to implement the prime number, I need the program to be able to make the program print all the pairs of prime numbers whose sum is the value of the specified value in the array.
Apr 13, 2016 at 11:32am
I'm not an algorithms expert, but a first-pass, brute force way of doing it would be:

1) Generate a list of all prime numbers less than the value you've chosen

2) Calculate the sum of every possible pair of prime numbers from that list

3) For each sum that is equal to the specified value, print the prime numbers that were added together to make that value.
Topic archived. No new replies allowed.