#include "stdafx.h"
#include <iostream>
usingnamespace std;
int matrix[3][3];
int main()
{
constint 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;
}
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.
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--;
}
}
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.
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.
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.
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:
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.
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.
}
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?
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.