Thread: Stuck on creating a program with array

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question Stuck on creating a program with array

    Hey there! I am a beginner with C++ and has now begun to learn me how arrays works but I'm stuck on a practice problem and the program them wants me to do is to:

    Write a program that takes in 50 values and prints out the highest, the lowest, the average and then all 50 input values, one per line.

    I am totally stuck at this practice problem the only code I could write was this:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int value[50];
        for (int i = 0; i < 50; i++)
        {
            cout << "Enter value " << i << ": \n";
            cin >> value[i];
        }
    }
    I'm do not know how to do so the program prints out which number was the highest, lowest and the averange and then all 50 input values. Can somebody help me how to do this?, Please.


  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I have a variable named max. I initialize it to a very big negative number. Let's say, -100.
    I ask you to give me 3 numbers.
    You give me the first, 3. I compare it with max and I choose the greatest of these two and I assign this to max. Now max is 3, since 3 > - 100.
    Then you give me 2. I compare 2 with max (which is 3) and I see that 2<max so I do nothing.
    You give me 10. I compare 10 with max(which is 10) and I see that 10>max so I assign ten to variable max.
    End of procedure.
    Max holds the greatest number you gave me.

    Try to implement this for a start
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What you should start with is the logic required for such a thing. Implement it in pseudo code or a flow chart.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question

    std10093: I read your comment and that helped me a little bit to get started by writing the code.

    But I'm still not knows exactly how to do the code I have written so far is:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int number[3];
        for (int i = 0; i < 3; i++)
        {
            cout << "Give me 3 numbers " << i << ": \n";
            cin >> number[i];
        }
        if (number[0] > number[1])
        {
            cin << number[0]; 
        }
        else if (number[1] > number[0])
        {
            cout << number[1]; 
        }
        for (int j = 0; j < 47; j++)
        {
            cout << '\n';
            cout << j;
        }
    }
    I knows i needs something that I need to compare how big and low all 3 numbers is so the program can now which number that is the higest and print it out first and then the lowest, the average. but I do not know how to write that can somebody give me a hint or something?.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    See above reply. Use a flowchart.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by DecoratorFawn82 View Post
    std10093: I read your comment and that helped me a little bit to get started by writing the code.

    I knows i needs something that I need to compare how big and low all 3 numbers is so the program can now which number that is the higest and print it out first and then the lowest, the average. but I do not know how to write that can somebody give me a hint or something?.
    I suggest re-reading the post, again.

    Tim S.

    Quote Originally Posted by std10093 View Post
    I have a variable named max. I initialize it to a very big negative number. Let's say, -100.
    I ask you to give me 3 numbers.
    You give me the first, 3. I compare it with max and I choose the greatest of these two and I assign this to max. Now max is 3, since 3 > - 100.
    Then you give me 2. I compare 2 with max (which is 3) and I see that 2<max so I do nothing.
    You give me 10. I compare 10 with max(which is 10) and I see that 10>max so I assign ten to variable max.
    End of procedure.
    Max holds the greatest number you gave me.

    Try to implement this for a start
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Ok it seems that you are trying, so read the code I provide below and refer to post #2 for the logic.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int max = -1000; // Initialize max with a really small value
        int input;
        
        // Now read three integers from input
        for(int i = 0 ; i < 3 ; i++)
        {
            cout << "Input" << endl;
            cin >> input;
            if(input > max)
            {
                max = input;
            }
        }
        
        cout << "Max is " << max << endl;
        
        return 0;
    }
    If you just copy paste the code, then you won't get the logic, thus you won't implement the code to find min. If you do what I said in first sentence of this post, then you will be able to do it. I hope you will
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't hand out solutions--especially logic!
    The OP must be able to come up with something as simple as this on his/her own, not guided by some invisible hand. The OP will learn nothing by being fed code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    It's a chance for him to get a boost and see how the logic I explained on post #2 is implemented. That can give a good student that is really stuck the chance to go on. If he is not in this group he simply copy paste the code and then nothing. Moreover, the answers I saw did not see enough to help. If I was him, I couldn't be helped by them.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by std10093 View Post
    ...That can give a good student that is really stuck the chance to go on.
    And help a student learn absolutely nothing.

    Moreover, the answers I saw did not see enough to help. If I was him, I couldn't be helped by them.
    What??? How is being able to make a simple flowchart too difficult or non-helping? This is basics of basics! This is something they absolutely should know. If they don't, then it's time to learn.
    The OP has made no effort in trying to demonstrate any effort into actually making a flowchart. Therefore, it is wrong to give out any sort of code.
    This is an exercise in itself. To take your problem, make a flowchart, then implement that code.
    Sure, the OP may not know how to translate a flowchart into code--we could help with that, if the OP has show willingness to actually do something him/herself.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    std10093: I have understand your code and it's working. What it does is of course output the highest number. But I must think of self how to do so the program knows what the lowest number is and the average. I got the code that prints out the highest and the lowest number and the code is this:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int max = -1000; 
        int lowest = 1000; 
        int input;
    
        for (int i = 1; i < 4; i++)
        {
            cout << "Input " << i << ": ";
            cin >> input;
            if (input > max)
            {
                max = input;
            }
            if (input < lowest)
            {
                lowest = input;
            }
        }
        cout << "Max is: " << max << '\n';
        cout << "Lowest is: " << lowest << '\n';
     
    return 0;
    }
    But have still problem with the average. I guess it has something with "max" to do if I'm not have understand this wrong.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Again, do a flowchart. Jot down how you'd do it in real life to get the hang of it.
    Don't avoid it. It's not hard, and it will help you a ton.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by std10093 View Post
    Ok it seems that you are trying, so read the code I provide below and refer to post #2 for the logic.

    ...

    If you just copy paste the code, then you won't get the logic, thus you won't implement the code to find min. If you do what I said in first sentence of this post, then you will be able to do it. I hope you will
    Give a man a fish, and he eats for a day. Teach a man....bla bla you know the rest.

    Besides, your solution is incorrect. What if only numbers below -1000 was entered? Then the program will say that the largest value entered is -1000, and the user will be calling OP in the middle of the night screaming for a refund. Also, your (faulty) solution uses magic numbers. If someone came across this code, without already knowing what it is supposed to be doing, the -1000 would be a mystery.

    So not only are you denying OP the opportunity to solve this problem for himself, you are providing him with a faulty and subpar solution that he will undoubtedly copy/paste without question.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    To help with the math.
    Average equals the sum divided by the number of items.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  15. #15
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Have still not figured out what to do in this case. The first ones to get the higest number and the lowest numbers are actually very easy to do it's just to initialize the variable max to a really low value and variable lowest to a really high value. It's just to use the signs < & > that means lesser and greater. But the third I do not know which sign I will use tried to use the / sign to divide something with the input values.

    stdhta01: Average equals the sum divided by the number of items. What are you meaning with number of items is it the number of how many input values?. Still stuck on this still needing more help with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck in a MPI program.
    By jeffwang in forum C Programming
    Replies: 2
    Last Post: 01-05-2012, 06:33 PM
  2. Stuck on a program
    By Northstar in forum C Programming
    Replies: 4
    Last Post: 10-12-2007, 03:02 AM
  3. stuck on array
    By quiet_forever in forum C++ Programming
    Replies: 6
    Last Post: 03-31-2007, 01:34 PM
  4. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  5. Stuck On Program
    By Robert Parker in forum C++ Programming
    Replies: 5
    Last Post: 06-04-2002, 01:41 PM