Thread: basic array problem

  1. #1
    Unregistered
    Guest

    basic array problem

    I am trying to complete a final project for an intro to c++ course. I need to find that maximum and minimum values of an array from user supplied numbers of the type float. How the heck do you do this?

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Just do something like this:

    maxsize = array[0];
    minsize = array[0];

    for(USHORT i=0; i<strlen(array); i++){
    if(array[i] > maxsize){
    maxsize = array[i];
    }
    else if(array[i] < minsize){
    minsize = array[i];
    }
    }

    Good luck!

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    66
    something like this the code probably isn't the best but it will work I hope
    #include <iostream.h>

    void main()
    {

    int x;
    float y;

    float array[10]; //don't know how many numbers you need sorry

    cout << "Please enter nine numbers with spaces in between them";
    cin >> array[0] >> array[1] >> array[2] >> array[3] >> array[4] >> array[5] >> array[6] >> array[7] >> array[8] >> array[9];

    //for highest number

    y = array[0]
    x=1;


    do
    {
    if(array[x] > y)
    {
    y = array[x];
    }
    x++;
    }
    while(x < 9);

    cout << y;

    y=array[0];
    x=1;

    do
    {
    if(array[x] < y)
    {
    y=array[x];
    }
    x++;
    }
    while(x < 9);

    cout << array[x];
    }



    hopefully that works

  4. #4
    Unregistered
    Guest

    kind of works

    crossbows example is more along the lines of where I'm trying to go. It seems to have worked great with getting me the max value but not the minimum.
    here's what I'm looking at:

    // Function to display data
    void data(float temp[], int &i)
    {

    float max = temp[0],
    min = temp[0],
    total = 0,
    avg;
    int tempNum = 0;
    for (i = 0; i < 5; i++)

    if (temp[i] > max)
    {
    max = temp[i];
    }
    if (temp[i] < min)
    {
    min = temp[i];
    }
    total += temp[i];
    avg = total / i;
    cout << "\nmax temperature was..." << max << endl;
    cout << "\nmin temperature was..." << min << endl;
    cout << "\nThe total number of temperatures was..." << i << endl;
    cout << "\nThe average temperature was..." << avg << endl;

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    You need to put parenthisis around the things in your for statement:

    void data(float temp[], int &i)
    {

    float max = temp[0],
    min = temp[0],
    total = 0,
    avg;
    int tempNum = 0;
    for (i = 0; i < 5; i++)
    {
    if (temp[i] > max)
    {
    max = temp[i];
    }
    if (temp[i] < min)
    {
    min = temp[i];
    }
    total += temp[i];
    avg = total / i;
    }
    cout << "\nmax temperature was..." << max << endl;
    cout << "\nmin temperature was..." << min << endl;
    cout << "\nThe total number of temperatures was..." << i << endl;
    cout << "\nThe average temperature was..." << avg << endl;
    }

    I think it should work then.

  6. #6
    Unregistered
    Guest

    worked great!

    that did it! Thanks a lot. Maybe I can get some sleep tonight after all! Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. help w/ array problem
    By rhythm313 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2002, 12:12 AM
  4. problem with an array of strings in C
    By ornamatica in forum C Programming
    Replies: 14
    Last Post: 05-01-2002, 06:08 PM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM