Thread: Array assignment

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    5

    Array assignment

    I have a project that requires I take user input from menu options and put it into an array which I will average out. I can set the menu up I think, but I cannot understand how to put what the user inputs into an array. Granted I just took the lecture on arrays today. Also we can only use functions to do the work. A little direction is all I need.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    int user_input;
    
    /* read user input here */
    
    array[i] = user_input;
    what's the problem?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    5
    I have to setup a menu that allows the user to input an integer between 1-5 for seven days:

    Please enter level for day 1:
    Please enter level for day 2:
    etc...

    the program will enter each entry into an array. Then take the array and average the elements and then the average has to be output to a corresponding color.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Did you have a specific question? vart already illustrated how to read input into an array (although you don't necessarily need an additional variable - you can read right into the array). Requesting the same input a few times in a row implies a loop of some kind.

    Rather than look at your entire assignment as one big task, break it down into smaller tasks and just focus on one at a time. First, write code to prompt for input, read that input, and print it out (I'm assuming you've already done some of these things). When that works, figure out how to repeat the prompt, and store the results in an array (each element of the array is its own variable), then print out the array. When that works, start the next part (averaging or whatever). Break the problem down into smaller problems and solve them (i.e. code, compile, and test) each one at a time.

  5. #5
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Code:
    double average(const int* arr, unsigned int siz)
    {
        assert(arr && siz);
        const double n = (double)siz;
        int sum = 0;
        do {
            sum += *arr++;
        } while( --siz );
        return (double)sum / n;
    }
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    @zub - While your desire to help is commendable, you should avoid providing complete solutions to new programmers - especially without any explanation of what the code does. We want to help people learn how to program, and that means giving them clues or advice so they can get to the final result on their own. Just giving out answers does not help the learning process.

  7. #7
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    He expressed his ideas so vague that I'm not sure my solution suitable for him. Most likely, he will have to understand the code and change it. Thus, it can be considered only a hint. Also, my style badly perceived by beginners. I am convinced that many times. Thus, he will have to study my code very carefully, checking every second with books. I'm sure it will bring more benefit to him than a hundred correct but vague advices.
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  8. #8
    Registered User
    Join Date
    Jul 2014
    Posts
    5
    I hear ya Matticus. Thanks for the code Zub, but there is no way I can get away with that. I am working on it piece by piece but I am way overwhelmed by the individual pieces. I will work on one module at a time. I have the first function pretty much worked out. Functions and arrays are totally new to me so they are a little hard to wrap my head around conceptually. I will post as I go along. Thank you for your responses thus far and I look forward to your further input.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by zub View Post
    He expressed his ideas so vague that I'm not sure my solution suitable for him. Most likely, he will have to understand the code and change it. Thus, it can be considered only a hint. Also, my style badly perceived by beginners. I am convinced that many times. Thus, he will have to study my code very carefully, checking every second with books. I'm sure it will bring more benefit to him than a hundred correct but vague advices.
    You make some good points, but your approach can go either way. Either the code can be studied and learned from, or blindly copied and used (I'm not suggesting the OP would do this - I'm making a generalized statement).

    In addition, if the latter occurs, there are several potential negative consequences.

    - Lack of a "figuring it out yourself" learning experience (the most valuable kind);
    - Bad grade if whoever is grading the program sees code that is beyond the abilities of the student.

    The first point is probably the most important. One can read from a source and think they have understanding, only to realize they don't when they no longer have something to reference. Struggling to develop these skills is the best way to master this knowledge.

    The second point can be bad either way - if they do get a good grade for something they didn't do themselves, they will advance to more difficult level and not be prepared since they didn't learn basics as well as they could have. Worst case scenario - they get through their education without proper knowledge and end up working in the field where they inhibit the work of others who know their stuff.

    On the other hand, the "guide them to the right answer" approach has none of these problems.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Cody Staus View Post
    I hear ya Matticus. Thanks for the code Zub, but there is no way I can get away with that. I am working on it piece by piece but I am way overwhelmed by the individual pieces. I will work on one module at a time. I have the first function pretty much worked out. Functions and arrays are totally new to me so they are a little hard to wrap my head around conceptually. I will post as I go along. Thank you for your responses thus far and I look forward to your further input.
    The best part about programming is how easy it is to create little "test programs" to play with concepts and get an understanding of them.

    Also, there's an example of building up a small program piece by piece here, if it helps to see an example -> A development process

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Please ... Array Assignment
    By CoolChica in forum C Programming
    Replies: 8
    Last Post: 04-07-2013, 07:41 PM
  2. Exception in Assignment of 2D array
    By sanddune008 in forum C Programming
    Replies: 4
    Last Post: 01-01-2012, 04:39 AM
  3. Pointer assignment using array name
    By stavos77 in forum C Programming
    Replies: 7
    Last Post: 03-12-2010, 03:12 PM
  4. Need help with an array assignment
    By 3wit in forum C Programming
    Replies: 1
    Last Post: 04-17-2008, 10:52 AM
  5. array assignment question
    By threahdead in forum C Programming
    Replies: 2
    Last Post: 08-17-2003, 05:36 PM