6 Replies - 361 Views - Last Post: 13 November 2019 - 06:43 AM Rate Topic: -----

#1 PendeJoe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 09-October 19

Help to understand!

Posted 10 November 2019 - 07:37 PM

Hey, i got a homework assignment that i dont really understand how to get started!
The objective is to generate 10 random integers between a low and high bound with the help of arrays.
I cant figure out how get the cin values from user to go through an array which then creates 10 random numbers within the range of the low and high bound, any advice?

My .cpp atm!

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
void programmerInfo();
using namespace std;

void printArray(const int[], int size);
void fillArray(int low[], int high[]);
void programmerInfo();

int main(){
    programmerInfo();
    int low, high, searchVal;
    cout << "***This program will generate 10 random integers between [LOW] and [HIGH]***" << endl;
    cout << "Please enter an integer value for the lower bound: ";
       cin >> low;
    cout << "Please enter an integer value for the higher bound: ";
        cin >> high;
    while(low>=high){
        cout << "The lower bound should be smaller than the higher bound!" << endl;
        cout << "Please enter and integer value for the higher bound: ";
            cin >> high;
    }

    cout << "Please enter an value between " << low << " and " << high << " to search: ";
        cin >> searchVal;
    while(low>searchVal||high<searchVal){
        cout << "The value to search for should be within the range." << endl;
        cout << "Please enter an value between " << low << " and " << high << " to search: ";
            cin >> searchVal;
    }

    cout << "The 10 random integers between " << low << " and " << high << " are:" << endl;



    srand((unsigned)time(NULL));


return 0;

}

void fillArray(int arr[]){
    for (int i = 0; i <= 10; i++){
        arr[i] = rand() % 10;
         }
}

void printFunc(int arr[]){
    for (int i = 0; i <= 10; i++){
        cout << arr[i] << endl;
    }
}

void programmerInfo(){
   cout << left << setw(17) << "Programmer Name:" << setw(7) << right << "John Smith\n";
   cout << left << setw(20) << "Programmer ID:" << setw(10) << right <<"1234567\n";
}[code/]

Sorry for rookie misstake, updated! 

[code]#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
void programmerInfo();
using namespace std;

void printArray(const int[], int size);
void fillArray(int low[], int high[]);
void programmerInfo();

int main(){
    programmerInfo();
    int low, high, searchVal;
    cout << "***This program will generate 10 random integers between [LOW] and [HIGH]***" << endl;
    cout << "Please enter an integer value for the lower bound: ";
       cin >> low;
    cout << "Please enter an integer value for the higher bound: ";
        cin >> high;
    while(low>=high){
        cout << "The lower bound should be smaller than the higher bound!" << endl;
        cout << "Please enter and integer value for the higher bound: ";
            cin >> high;
    }

    cout << "Please enter an value between " << low << " and " << high << " to search: ";
        cin >> searchVal;
    while(low>searchVal||high<searchVal){
        cout << "The value to search for should be within the range." << endl;
        cout << "Please enter an value between " << low << " and " << high << " to search: ";
            cin >> searchVal;
    }

    cout << "The 10 random integers between " << low << " and " << high << " are:" << endl;



    srand((unsigned)time(NULL));


return 0;

}

void fillArray(int arr[]){
    for (int i = 0; i <= 10; i++){
        arr[i] = rand() % 10;
         }
}

void printFunc(int arr[]){
    for (int i = 0; i <= 10; i++){
        cout << arr[i] << endl;
    }
}

void programmerInfo(){
   cout << left << setw(17) << "Programmer Name:" << setw(7) << right << "John Smith\n";
   cout << left << setw(20) << "Programmer ID:" << setw(10) << right <<"1234567\n";
}


Is This A Good Question/Topic? 0
  • +

Replies To: Help to understand!

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,583
  • Joined: 12-June 08

Re: Help to understand!

Posted 11 November 2019 - 10:42 AM

I would think you would want to add two parameters here. One for the high and one for the low values.
045   void fillArray(int arr[]){

Was This Post Helpful? 0
  • +
  • -

#3 PendeJoe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 09-October 19

Re: Help to understand!

Posted 11 November 2019 - 04:41 PM

Yeah i figured out that i was missing parameters ! Ty :)/>

This post has been edited by Skydiver: 11 November 2019 - 08:41 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 PendeJoe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 09-October 19

Re: Help to understand!

Posted 11 November 2019 - 07:14 PM

okey so i just finished my homework, and i would love someone to give me some feedback if you have the time :)/>

Requirement:

Input Validation: Do not accept the lower bound value is greater or equal to the higher bound. The search value should be within the range.
Write functions to generate the random numbers and store in an array, find the greatest value, the smallest value and average from the array.
Display the programmer info at the beginning of the output
Include the function prototypes before the main()
All functions definitions should be after the main()

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
using namespace std;

void fillArray(int low, int high, int index, int numbers[]);
double average(int numbers[], int index);
bool searchValue(int numbers[], int index, int value);
int maximum(int numbers[], int index);
int minimum(int numbers[], int index);
void programmerInfo();

int main(){
    programmerInfo();
    int low, high, searchVal;
    int index=10;
    int value;
    int numbers[10];

    cout << "***This program will generate 10 random integers between [LOW] and [HIGH]***" << endl;
    cout << "Please enter an integer value for the lower bound: ";
       cin >> low;
    cout << "Please enter an integer value for the higher bound: ";
        cin >> high;
    while(low>=high){
        cout << "The lower bound should be smaller than the higher bound!" << endl;
        cout << "Please enter and integer value for the higher bound: ";
            cin >> high;
    }

    cout << "Please enter an value between " << low << " and " << high << " to search: ";
        cin >> searchVal;
    while(low>searchVal||high<searchVal){
        cout << "The value to search for should be within the range." << endl;
        cout << "Please enter an value between " << low << " and " << high << " to search: ";
            cin >> searchVal;
    }

    cout << "\nThe 10 random integers between " << low << " and " << high << " are:" << endl;

    srand((unsigned)time(0));
    fillArray(low, high, index, numbers);
    for(int i=0;i<index;i++){
        cout << setw(5) << right << numbers[i];
    }

    cout << "\nThe greatest value from the random numbers is, " << maximum(numbers, index) << endl;
    cout << "The smallest value from the random numbers is, " << minimum(numbers, index) << endl;
    cout << "The average from the random numbers is, " << setprecision(2) << fixed << average(numbers, index) << endl;

    value = searchValue(numbers, index, searchVal);
    if(value==1){
        cout << searchVal << " was found from the random numbers!";
    }else
        cout << searchVal << " can't be found from the random numbers!";


return 0;
}

void fillArray(int low, int high, int index, int numbers[]){
    for (int i = 0; i < index; i++)
        numbers[i] = rand() % (high-low +1) + low;
}

double average(int numbers[], int index){
    double average=0;
    for(int i=0;i<index;i++){
        average += numbers[i];
    }
    average = average / index;
return average;
}

int maximum(int numbers[], int index){
    int large=numbers[0];
    for(int i=0;i<index;i++){
        if(numbers[i]>large)
            large=numbers[i];
    }
return large;
}

int minimum(int numbers[], int index){
    int small=numbers[0];
    for(int i=0;i<index;i++){
        if(numbers[i]<small)
            small=numbers[i];
    }
return small;
}

bool searchValue(int numbers[], int index, int value){
    for(int i=0;i<index;i++)
        if(numbers[i]==value)
        return true;
}

void programmerInfo(){
   cout << left << setw(17) << "Programmer Name:" << setw(7) << right << "John Smith\n";
   cout << left << setw(20) << "Programmer ID:" << setw(10) << right <<"1234567\n";
}

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7136
  • View blog
  • Posts: 24,244
  • Joined: 05-May 12

Re: Help to understand!

Posted 11 November 2019 - 08:40 PM

What happens to your searchValue() if there is no match? What is the return value?
Was This Post Helpful? 1
  • +
  • -

#6 PendeJoe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 09-October 19

Re: Help to understand!

Posted 13 November 2019 - 12:24 AM

Thanks for the feedback! took the time and noticed that i forgot to set the value to false if the true value wasn't found ! :)/>

This post has been edited by Skydiver: 13 November 2019 - 06:42 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7136
  • View blog
  • Posts: 24,244
  • Joined: 05-May 12

Re: Help to understand!

Posted 13 November 2019 - 06:43 AM

You're welcome.

There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1