So at this point I am lost.
My teacher wants us to generate an array that contains random positive integers that will allow the user to do different things with those integers. such as REVERSE, ADD, SEARCH and EXIT the program, which then returns the user back to the beginning to start over.
To reverse the array,
I tried finding tutorials and solutions on how to reverse an array but it confused me more when it came to how to use it in my program. so the compiler is currently giving me the following errors:
line 23 col 28 [Error] request for member 'length' in 'normalArray', which is of non-class type 'int'
line 47 col 50 [Error] invalid types 'int[int]' for array subscript
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
int normalArray;
void linearSearch( constint [], int, int );
char option; //declaring options R, A, S, E the users will select
int length=normalArray.length();
int reverseArray[length];
cout << "-------------------------------------------------------" << endl;
srand(time(0));
int search, array[10]; //
for ( int x = 0; x < 10; x++ ) //
{
cout << setw(3) << ( rand() % (100 - 60 +1) + 60);// Prints out 9 random numbers between 60 and 100
int srand[10] = {rand() % 40 + 61}; //puts a number into array
}
cout << "\n R[everse] A[dd] S[earch] E[xit]";
cout << "\n Choose what to do with the random array of numbers: ";
cin >> option;
if(option == 'R'|| option =='r')
{
for(int count=0; count<10; count++)
{
reverseArray[count]=normalArray[length-1];
length--;
}
cout << "The reverse version of the array = " << reverseArray << endl;
}
/*--------------------------------------------------------------*/
elseif (option == 'A' || option == 'a')
cout<< "The sum = " << endl;
/*---------------------------------------------------------------*/
elseif (option == 'S' || option == 's')
{
cout << "Please enter a number to search for "<< endl;
cin >> search;
cout << "The value of that element is: " << search << endl;
}
/*--------------------------------------------------------------------*/
while (option !='E' && option != 'e');
cout << "End of program";
return 0; //indicates a successful termination
} // end main
#include <iostream>
#include <random>
void initializeArrayRandomly(int num[], int size);
int randomChoice(int min, int max);
int main()
{
constint ELEMENTS{ 10 };
int numbers[ELEMENTS] = { 0 };
initializeArrayRandomly(numbers, ELEMENTS);
}
void initializeArrayRandomly(int num[], int size)
{
std::cout << "Entering the following integer numbers (randomly):\n";
for (int count = 0; count < size; count++)
{
std::cout << "Row # " << count + 1 << " : ";
num[count] = randomChoice(60, 100); // Call the random function and set the min, max values to generate random numbers.
std::cout << num[count] << std::endl; // Display the array with random numbers.
}
}
// Set up the random number Generator (C++ 11).
int randomChoice(int min, int max)
{
std::mt19937 generator(std::random_device{}());
std::uniform_int_distribution<int> distributionToss(min, max); // Set the numbers for int.
constint randNumber = distributionToss(generator);
return randNumber;
}
line 25: invalid conversion from 'int' to 'int*' [-fpermissive]
line 17: [note] initializing argument 1 of 'void intializeArrayRandomly(int*, int)'
line 88: mt19937 is not a member of 'std'
line 88: random_device is not a member of std
line 89: uniform_int_distribution is not a member of std
line 89: expected primary-expression before int
line 90: generator was not declared in this scope
line 90: distributionToss cannot be used as a function
/*
Kaylah Hubbard
April 26, 2016
ELET 2300 - Assignment 2
Program description: This is a program that generates
an array filled up with random positive integer number ranging
from 60 to 100, and displays it on the screen.
*/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
usingnamespace std;
void intializeArrayRandomly(int numbers[], int size); //
int randomChoice(int min, int max); //
int main()
{
int numbers;
constint ELEMENTS{ 10 };
int number[ELEMENTS] = { 0 };
intializeArrayRandomly(numbers, ELEMENTS);
void linearSearch( constint [], int, int );
char option; //declaring options R, A, S, E the users will select
cout << "-------------------------------------------------------" << endl;
srand(time(0));
int search, array[10]; //
int x;
for ( int x = 0; x < 10; x++ ) //
{
cout << setw(3) << ( rand() % (100 - 60 +1) + 60);// Prints out 9 random numbers between 60 and 100
int srand[10] = {rand() % 40 + 61}; //puts a number into array
}
cout << "\n R[everse] A[dd] S[earch] E[xit]";
cout << "\n Choose what to do with the random array of numbers: ";
cin >> option;
if(option == 'R'|| option =='r')
{
for(int count=0; count<10; count++)
{
}
cout << "The reverse version of the array = " << endl;
}
//*************************************************************************************
elseif (option == 'A' || option == 'a')
cout<< "The sum = " << endl;
//*************************************************************************************
elseif (option == 'S' || option == 's')
{
cout << "Please enter a number to search for "<< endl;
cin >> search;
cout << "The value of that element is: " << search << endl;
}
//*************************************************************************************
while (option !='E' && option != 'e'); // Should be (option !='E' && option != 'e')
cout << "End of program";
return 0; //indicates a successful termination
} // end main
void intializeArrayRandomly (int num[], int size)
{
int count;
cout << "Row # " << count + 1 << " : ";
num[count] = randomChoice(60, 100); //random function called
cout << num[count] << endl; //display of random numbers
}
int randomChoice(int min, int max)
{
int distributionToss;
std::mt19937 generator(std::random_device{}());
std::uniform_int_distribution<int> distributionToss(min, max); //sets numbers for int
constint randNumber = distributionToss(generator);
return randNumber;
}
Write a program that generates an array filled up with random positive integer number
ranging from 60 to 100, and display it on the screen.
After the creation and displaying of the array, the program displays the following:
[R]everse [A]dd [S]earch E[xit]
Please select an option:_
Then, if the user selects:
- R (lowercase or uppercase): the program displays the reversed version of the array.
- A (lowercase or uppercase): the program displays the sum of the elements of the array.
- S (lowercase or uppercase): the program asks the user to insert an integer number and
look for it in the array, returning a message whether the number is found and its position
(including multiple occurrences, see example below), or is not found.
- E (lowercase or uppercase): the program exits.
NOTE: Until the last option (āeā or āEā) is selected, the main program comes back at the
beginning, asking the user to insert a new integer number.
---------------------------- end assignment ------------------------------------------------
intializeArrayRandomly(numbers, ELEMENTS);
What kind of object is numbers? What kind of object does this function expect as the first parameter?
1 2 3
int distributionToss;
std::mt19937 generator(std::random_device{}());
std::uniform_int_distribution<int> distributionToss(min, max); //sets n
On the first line there, you're creating an object named distributionToss. On the third line there, you're creating an object named distributionToss. This is a problem. You can't create two objects with the same name.
@ kaylah93, Moschops explained it better than I could. Furthermore, the reason I gave you a little hint with the random numbers is to change the old format:
1 2 3 4 5 6 7 8
srand(time(0));
int search, array[10]; //
int x;
for ( int x = 0; x < 10; x++ ) //
{
cout << setw(3) << ( rand() % (100 - 60 +1) + 60);// Prints out 9 random numbers between 60 and 100
int srand[10] = {rand() % 40 + 61}; //puts a number into array
}
to the one I typed above.
Also, be careful when you are naming the variables/objects. On line 23 and line 25 has very similar name : number and numbers. Although C++ will let you do it (since it is not the same), it could cause troubles later on when you are writing hundreds of line of code.