array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • masqwerty16
    New Member
    • May 2015
    • 8

    array

    #include<iostre am.h>

    main ()

    {
    int number[10] = {0,1,2,3,4,5,6, 7,8,9};
    int x;

    for (x=0; x<20; x++)

    {
    cout << "Input number (only input 0-9): " << endl;
    cin >> number[x];
    }

    for (x=0; x<10; x++)

    {
    cout << "Occurency of " << number[x] << endl;

    }
    return 0;

    }

    i don't have enough knowledge for array.. so help in this problem thanks.. the problem is "the user must input 20 number but only 0-9. after the user input 20 numbers the occurency of number 0-9 must be calculated how many did the user input 0-9.. please help me guys.. :D
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The first problem is that the loop where you enter the data goes from 0 to <20. There are only 10 elements in the array you have defined..

    The second problem is that the second loop goes from 0 to 10, which is correct as far as you array is concerned but the problem says to enter 20 numbers. So your array should be 20. In any case be sure the loops cover the same number of elements as your array.

    The third problem is that you have no check when the data is entered if the value is 0-9. You will need a loop to verify the 0-9 and then put the value in the array is it is between 0-9. That loop will be inside the loop you already have for data entry.

    The fourth problem is that you do not total the values of the elements. If the values are 0-9 you will need 10 int counters. Then in your display loop insert a switch statement where each case adds to the corresponding counter. After the loop has gone through the entire array, then display the counters. All you do so far is to display the array elements.

    Read this for more info on how to use arrays: http://bytes.com/topic/c/insights/77...rrays-revealed

    Comment

    Working...