Help with Arrays

This is a discussion on Help with Arrays within the C++ Programming forums, part of the General Programming Boards category; Can anyone help with a program that would allow you to enter 5 floating point values into an array called ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    15

    Help with Arrays

    Can anyone help with a program that would allow you to enter 5 floating point values into an array called Marks and then calculate and displays the average of the marks entered?

    Also need help with a second program that manipulates an array of 7 integers according to the following rules:

    first element is 32, last element is 87 with the second element being one more that of the last element. The second last element is 10 less than the first element and the 4th element is the sum of the first two and the last two elements, and the 3rd and the 5th elements are both zero.

    Can anyone help me out with these and explain how it would work. Thanks

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    This is all I have based on one example for the 2nd program can someone tell me what I'm doing worng


    Code:
    #include <iostream>
    
      using namespace std;
    
    int sum( int numbers[], int length ); 
    
    int main()
    
    {
    int numbers [32];        
    for(int i = 32; i < 87; i++)
    {
    cout << "Enter a value for element " << i << " ";
    cin >> numbers [i];
    }
    int total = sum(numbers, 87 ); /
    cout << "The sum of the array is " << total << endl;
    }
    int sum( int numbers [], int length )
    {
    int total = 0;
    for( int i = 0; i < length; i++ )
    total += numbers [i];
    return total;
    cin.get();
    cin.ignore();
     }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    For the 2nd program. Fill in the blank:
    Code:
       array[0] = XX;
       array[6] = XX;
       array[1] = XX + 1;
       array[5] = XX - 10;
       array[2] = 0;
       array[4] = 0;
       array[3] = XX + XX + XX + XX;

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    Code:
     
    array[0] = 32;
       array[6] = 87;
       array[1] = 87 + 1;
       array[5] = 32 - 10;
       array[2] = 0;
       array[4] = 0;
       array[3] = 32 + 88 + 22 + 87;

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    Not sure how to how or what the program is to output.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You should write those in terms of the other array elements, with the exception of the first and last array elements. After that, make a for-loop to print them out, and you're done.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And for your first program, average is the sum divided by the number of elements, so you would have:
    Code:
    cout << "The average of the array is " << total / n << endl;
    n being the number of elements.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    I'm as lost as ever on those problems. Not sure were to begin or end. The help so far has been great but I'm not sure how to even introduce it in to the program

  9. #9
    C++ Beginner
    Join Date
    Jun 2005
    Posts
    39
    Can anyone help with a program that would allow you to enter 5 floating point values into an array called Marks and then calculate and displays the average of the marks entered?

    Also need help with a second program that manipulates an array of 7 integers according to the following rules:

    first element is 32, last element is 87 with the second element being one more that of the last element. The second last element is 10 less than the first element and the 4th element is the sum of the first two and the last two elements, and the 3rd and the 5th elements are both zero.


    Can anyone help me out with these and explain how it would work. Thanks

    I think I understand what you want... For the first program a basic code would be.

    Code:
    #include <iostream>
    float marks[5]; //makes mark array hold 5 varaibles
    
    cout << "Enter five floating values, and I will calculate and average them out." << endl; 
    cout << "Enter the first number\n" << endl;
    cin >> marks[0]; //puts in first varaible 
    cout << "Enter the second number\n" << endl;
    cin >> marks[1]; //puts in 2nd varaible
    cout << "Enter the third number\n" << endl;
    cin >> marks[2]; //puts in 3rd varaible
    cout << "Enter the fourth number\n" << endl;
    cin >> marks[3]; //puts in 4th varaible
    cout << "Enter the fifth number\n" << endl;
    cin >> marks[4]; //puts in 5th varaible
    
    for (i = 0, i < 5, i++) //as long as vaiable i is less than 5 it will loop
    {
    float number = number + marks[i] /*makes float number add marks value to it until 5th one */ 
    i = i + 1 //adds to varaible i
    if (i = 5) //if i equals 5 it calculates
    {
    float number = number \ 5 //divides by 5 for average
    }
    }
    
    cout << "The average of those 5 float values are:" << number << endl;
    
    return 0;
    }
    As for your second one I ahve no idea what your talking about
    I'm a beginner C++ programmer, but I have studied HTML and Java. So if you need to help me I should catch on fast =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21