I need to create a program that does this, but so far I have been getting nothing but errors. I've been having a friend look my code over but she is stumped too.
the instructions from my professor:
In the main function display a menu to get user’s option for:
1. Create and initialize a dynamic array
2. Display statistics on the array
3. Search for an element
4. Quit
The user should choose 1 first to proceed with the rest of the options.
When option 1 is chosen, ask the user for an integer value for size from inside the main function. This value will be the size of the array that you will create dynamically. Then call a function with the following prototype and description:
int *createArray(int &size) : Dynamically allocate and initialize with random numbers an array of size size. The parameter to this function is a reference variable which will store the size of the array. The function will return a pointer to the dynamically allocated array. In the main function, display contents of the array.
When option 2 is chosen, call a function with the following prototype and description:
void findStats(int *arr, int size, int &min, int &max, double &average) : This function finds the statistics about the array by searching the minimum and maximum elements and by calculating the average of elements (The average is calculated by adding each element value in the array then dividing this sum by the size of the array).
The first parameter to the function is a pointer to the dynamically created array, the second parameter is the size of the array that is passed by value, the third parameter is the minimum value that is passed by reference, the fourth parameter is the maximum value that is passed by reference, and the last parameter is the average value that is passed by reference. The findStats function returns a void, i.e. nothing. Still, since min, max, and average are passed by reference, you should be able to display these variables’ calculated values from inside the caller (main) function. Display the value of each statistic properly in the caller function.
When option 3 is chosen, get an integer value for an element from inside the main function. Then call a function with the following prototype and description:
int *searchElement(int *arr, int size, int *element): Search for the element stored in the variable element inside the array. The first parameter to the function is a pointer to the array. The second parameter is the size of the array. The third parameter is a pointer to the element variable for which you’re going to search. You may either use linear or binary search here, whichever is most convenient for you, but if you use a binary search, you must sort the array. The function returns a pointer to the element found in the array, or a 0 if the element is not found to the main function. Please display properly where the element is found (if found), or display not found (if the element is not found) in the main function.
using namespace std;
//This program is used to the ability to use pointers
//to handle arrays and variable locations,
//and accomplish dynamic memory allocation
//function prototypes
int *createArray(int) ;
void findStats(int, int, int, int, double);
int *searchElement(int, int, int);
int main(){
int choice, size, element, min, max;
int *arr;
double average;
cout << " choose one of the options" << endl;
cout << " 1. Create and initialize a dynamic array " << endl;
cout << " 2. Display statistics on the array" << endl;
cout << " 3. Search for an element" << endl;
cout << " 4. Quit" << endl;
cin >> choice;
if (choice = 1){
cout <<"what is the size of the array?" << endl;
cin >> size;
cout << " here are the contents of the array" << endl;
cout << *createArray(size);
}
else if (choice = 2){
findStats(arr, size, min, max, average); //call
cout << "Min = " << *min << endl;
cout << "Max = " << *max <<endl;
cout << "Average = " << *average << endl;
}
else if (choice = 3){
cout << "Enter a number to search for in the array: " << endl;
cin >> element;
searchElement(arr, size, element);// call the option
int arr[size],found;
found = searchElement(arr, size, element);
if(found==1)
cout<<"\nItem found";
else
cout<<"\nItem not found";
}
else if (choice = 4){
cout << "Quit"<< endl;
}
//free memory
delete [] arr;
arr[] = 0;
return 0;
}
int *createArray(int &size)
{
int *arr;
int num = 0;
cout << "Enter in a random number: ";
cin >> num;
int *createArray; // array to hold the numbers
// return null if number is 0 or negative
if (size <= 0)
return NULL;
// Dynamically allocate the array
arr = new int[size];
//seed random number
srand( time(0) );
// populate array with numbers
for (int size = 0; size < num; size++)
arr[size] = rand();
//return a pointer to the array
return arr;
}
void findStats(int *arr, int size, int &min, int &max, double &average) {
for (int i = 1; i < size; ++i) {
if (*arr[max] > *arr[i]) {
max = i;
}
}
min = 0;
for (int i = 1; i < size; ++i) {
if (*arr[min] < *arr[i]) {
min = i;
}
}
average = 0;
for (int i = 1; i < size; ++i) {
average += *arr[i];
}
average == average/size;
}
int *searchElement(int *arr, int size, int *element)
{
for(int I = 0; I < size; I++)
{
if(*arr[I] == *element)
return 1;
}
return 0;
}
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
usingnamespace std;
//This program is used to the ability to use pointers
//to handle arrays and variable locations,
//and accomplish dynamic memory allocation
//function definition
int *createArray(int &size)
{
int *arr;
int num = 0;
cout << "Enter in a random number: ";
cin >> num;
int *createArray; // array to hold the numbers
// return null if number is 0 or negative
if (size <= 0)
return NULL;
// Dynamically allocate the array
arr = newint[size];
//seed random number
srand( time(0) );
// populate array with numbers
for (int size = 0; size < num; size++)
arr[size] = rand();
//return a pointer to the array
return arr;
}
void findStats(int* arr, int size, int &min, int &max, double &average) {
for (int i = 1; i < size; ++i) {
if (arr[max] > arr[i]) {
max = i;
}
}
min = 0;
for (int i = 1; i < size; ++i) {
if (arr[min] < arr[i]) {
min = i;
}
}
average = 0;
for (int i = 1; i < size; ++i) {
average += arr[i];
}
average = average/size;
}
int searchElement(int* arr, int size, int element)
{
for(int I = 0; I < size; I++)
{
if(arr[I] == element)
return 1;
}
return 0;
}
int main(){
int choice, size=0, element, min, max;
int *arr;
double average;
cout << " choose one of the options" << endl;
cout << " 1. Create and initialize a dynamic array " << endl;
cout << " 2. Display statistics on the array" << endl;
cout << " 3. Search for an element" << endl;
cout << " 4. Quit" << endl;
cin >> choice;
if (choice = 1){
cout <<"what is the size of the array?" << endl;
cin >> size;
cout << " here are the contents of the array" << endl;
cout << *createArray(size);
}
elseif (choice = 2){
findStats(arr, size, min, max, average); //call
cout << "Min = " << min << endl;
cout << "Max = " << max <<endl;
cout << "Average = " << average << endl;
}
elseif (choice = 3){
cout << "Enter a number to search for in the array: " << endl;
cin >> element;
searchElement(arr, size, element);// call the option
int* arr= newint[10],found;
found = searchElement(arr, size, element);
if(found==1)
cout<<"\nItem found";
else
cout<<"\nItem not found";
}
elseif (choice = 4){
cout << "Quit"<< endl;
}
//free memory
delete [] arr;
arr = NULL;
return 0;
}
Thank so much for your help! I understand my knowledge of pointers isn't very good, I'll look over the tutorial sites you have provided. Is there anything else that can help me get better at this?
The only way for you to get better is practise practise and more practise. :P Really just think of little programs that you can make for yourself and try to use pointers in it has much as you can. While doing this keeping the Tutorial on C++ from this website or the textbook you have from school close by so when things don't work you can look at the book to see where you went wrong. :)
int array[4] = {10,12,13,14};
//array[0] == 10, array is a pointer to the address of the 0'th element, so
int *pointer = array; // will point to the 0'th element's ADDRESS!
the (*) means it points the the address so if you output the pointer now it
will display something like 0x28fedc, this is an address it could be any number like 100 (don't change it to 100, this is figuratively speaking)..
1 2
//i.e
std::cout<<pointer;
imagine the addresses were from say 100, an arrays elements increase 4 bytes for ints, 1 byte for chars e.t.c..
A visual representation of an array..
=======
= 10 =address 100 <--- pointer
=======
= 12 =address 104
=======
= 13 =address 108
=======
= 14 =address 112
=======
in the case of above doing.. std::cout<<pointer;
will output 100..
if you output the pointer again with the (*) it will deference it and display the actual value 10 rather than the address. std::cout<<*pointer;
if you increment the address, if it's an integer it will increment 4 bytes automatically..