Hello all. I wrote a program that gives me 100 random values from the range between -100 to +100. How am I supposed to find the minimum, maximum, and average of the values that will be listed when I run the program? I appreciate the help anyone can give me. Also, this is for a class and we are not allowed to use Arrays. Thanks again.

Here is what I have so far:

    float random_integer;
    float lowest=-100.00, highest=100.00;
    float range=(highest-lowest)+1;

for(float index=0; index<100; index++) {

random_integer = lowest+float(range*rand()/(RAND_MAX + 1.0));
cout << random_integer << endl; }



float sum;
float average;
float minimum, maximum;

minimum= (not sure what to put here...);

cout << ("Minimum:")<<minimum  << endl;

Recommended Answers

All 7 Replies

If I read out to you 100 numbers, asking you to tell me what the lowest number was when I've finished, how would you do it? In your head, without writing anything down.

This might work for you:

    float random_integer;
    float lowest=-100.00, highest=100.00;
    float range=(highest-lowest)+1;
    float sum = 0;
    float average;
    float minimum = highest, maximum=lowest;
    for(float index=0; index<100; index++)
    {
        random_integer = lowest+float(range*rand()/(RAND_MAX + 1.0));
        if (random_integer > maximum)
            maximum=random_integer;
        if (random_integer < minimum)
            minimum = random_integer;
        sum += random_integer;

        cout << random_integer << ",";
    }
    average = sum/100;
    cout <<endl<<endl<< ("Minimum: ")<<minimum << endl;
    cout <<"Maximum: " << maximum <<endl;
    cout <<"Sum: " << sum << endl;
    cout <<"Average: " << average;

I figured it out. Also, I forgot to mention that I have to prompt the user to enter either "data.txt" or "limits.txt". If the user enters "limits.txt", then the program saves a text file that has the min and max values of the set. If the user enters "data.txt", then the program saves a text file that has all the values of the set. How do I set up an if/else statement telling the program "if the user enters "data.txt", put the 100 values into a text file. If the user enters "limits.txt, put the min and max values into a text file."

Here is the code I have so far:

float random_integer;
        float lowest=-98.999, highest=98.999;
        float range=(highest-lowest)+1;

    for(float index=0; index<98.999; index++) {

    random_integer = lowest+float(range*rand()/(RAND_MAX + 1.1));
    cout << random_integer << endl; }



float minimum = 999;
float maximum = -999;
float counter = 0;
float average;
float total = 0;

for(float index=0; index<100; index++)
{
random_integer = lowest+float(range*rand()/(RAND_MAX + 1.1));
cout << random_integer << endl;
if (random_integer < minimum)
{
minimum = random_integer;
}
if (random_integer > maximum)
{
maximum = random_integer;
}
counter += 1;
total += random_integer;
}
average = total / counter;

    cout << ("Minimum:")<<minimum  << endl;
    cout << ("Maximum:")<<maximum  << endl;
    cout << ("Average:")<<average  << endl;
    cout << ("Samples:")<<100 << endl;


 string filename;
    cout << "Enter filename to save (eg. file.txt): ";
    cin >> filename;

    ofstream myfile;
    myfile.open(filename.c_str());
    myfile << minimum << endl;
    myfile << maximum << endl;
    myfile.close();

C++ string objects can be compared with the equals operator, the return value is true or false, so it can be used as the conditional of an if statement.

string input;
string a;
string b;
if(input == b)
else if (input == a)
else

Instead of having the user type it in, you could try using a menu, that the user can just select which one they want.

#include <iostream>
using namespace std;
void SaveData();
int main()
{
    int MenuChoice = 1;
    while(MenuChoice != 0)
    {
        cout << "\tMain Menu" << endl;
        cout << "1. Save All Data"<< endl<< "2. Save Min and Max" << endl << "0. Quit" << endl << "Menu Choice: ";
        cin >> MenuChoice;
        cout << endl;
        switch (MenuChoice)
        {
        case 1:
            SaveData("data.txt");
            break;
        case 2:
            SaveData("limits.txt");
            break;
        case 0:
            break;
        default:
            cout << "Choose only 0-2 please"<< endl;
        }
    }

    return 0;
}
void SaveData(string filename)
{
    ofstream myfile;
    myfile.open(filename.c_str());
    if(filename == "data.txt")
    {

    }
    else
    {

    }
    myfile.close();    
}

I used your idea to make this, but it says "filename is undefined" when I try to compile. The error is is where it says "cin >> filename;"

#include "stdafx.h" 
#include <windows.h>
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;

float main() {

{
    srand((unsigned)time(0));

        float random_integer;
        float lowest=-98.999, highest=98.999;
        float range=(highest-lowest)+1;

    for(float index=0; index<98.999; index++) {

    random_integer = lowest+float(range*rand()/(RAND_MAX + 1.1));
    cout << random_integer << endl; }



float minimum = 999;
float maximum = -999;
float counter = 0;
float average;
float total = 0;

for(float index=0; index<100; index++)
{
random_integer = lowest+float(range*rand()/(RAND_MAX + 1.1));
cout << random_integer << endl;
if (random_integer < minimum)
{
minimum = random_integer;
}
if (random_integer > maximum)
{
maximum = random_integer;
}
counter += 1;
total += random_integer;
}
average = total / counter;

    cout << ("Minimum:")<<minimum  << endl;
    cout << ("Maximum:")<<maximum  << endl;
    cout << ("Average:")<<average  << endl;
    cout << ("Samples:")<<100 << endl;


 void SaveData(string filename);
 {
  cout << "Enter filename to save (eg. file.txt): ";
   cin >> filename;

    ofstream myfile;
    myfile.open(filename.c_str());
    if(filename == "data.txt")
    {
         SaveData("data.txt");

    }
    else
    {
        SaveData("limits.txt");
    }
    myfile.close();
 }
//    myfile << minimum << endl;
//    myfile << maximum << endl;
//   myfile.close();

//    if (myfile<"data.txt") {
//}

//else { 

//    ofstream myfile;
//    myfile.open(filename.c_str());
//    myfile << random_integer << endl;
//    myfile.close();

//}




system("pause");
}
    }

try this:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <ctime>

using namespace std;
void SaveData(string filename);
void GenerateData();
void DisplaySummary();
float random_integer;
float lowest=-98.999, highest=98.999;
float range=(highest-lowest)+1;
float sum = 0;
float average = 0;
float minimum = highest, maximum=lowest;
string DataFile = "data.txt";
string LimitsFile = "limits.txt";
int main()
{
    srand((unsigned)time(0));
    char MenuChoice = '1';
    while(MenuChoice != '0')
    {
        cout << "\tMain Menu" << endl;
        cout << "1. Generate Data"<< endl;
        cout << "2. Display Data Summary"<< endl;
        cout << "3. Save All Data"<< endl;
        cout << "4. Save Min and Max" << endl;
        cout << "0. Quit" << endl;
        cout << "Menu Choice: ";
        cin >> MenuChoice;
        cout << endl;
        switch (MenuChoice)
        {
        case '1':
            GenerateData();
            break;
        case '2':
            DisplaySummary();
            break;
        case '3':
            SaveData("data.txt");
            break;
        case '4':
            SaveData("limits.txt");
            break;
        case '0':
            break;
        default:
            cout << "Choose only 0-4 please"<< endl;
        }
    }
    return 0;
}

void SaveData(string filename)
{
    ofstream myfile;
    myfile.open(filename.c_str());
    if(filename.compare("data.txt") == 0)
    {
        myfile <<endl<<endl<< "Minimum: "<<minimum << endl;
        myfile <<"Maximum: " << maximum <<endl;
        myfile <<"Sum: " << sum << endl;
        myfile <<"Average: " << average;
    }
    else
    {
        myfile << endl << endl << "Minimum: " << minimum << endl;
        myfile << "Maximum: " << maximum << endl;
    }
    myfile.close();
    cout << endl << "Data Saved" << endl << endl;
}

void GenerateData()
{
    for(float index=0; index<100; index++)
    {
        random_integer = lowest+float(range*rand()/(RAND_MAX + 1.0));
        if (random_integer > maximum)
            maximum=random_integer;
        if (random_integer < minimum)
            minimum = random_integer;
        sum += random_integer;
        cout << random_integer << ",";
    }
    cout << endl << endl;;
    average = sum/100;
}

void DisplaySummary()
{
    cout << endl << endl << "Minimum: "<<minimum << endl;
    cout <<"Maximum: " << maximum <<endl;
    cout <<"Sum: " << sum << endl;
    cout <<"Average: " << average << endl << endl;
}

If you have any questions about any part of this let me know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.