Need help! supposed to take in 6 project grades and 2 test grades... multiply each by %50 and add together for final average. however, i cant figure out how to add the integers in the array much less compute the average... any help is highly appreciated!!!

heres the code:

#include <iostream>

using namespace std;

int main(void) {

    cout <<endl;
    cout << "CSE 2050 Program 1" << endl; 
    cout << "Arun Chandra" << endl; 


int proj[6]; //creates an array for project grades
int test[2]; // creats an array for test grades

float psum;// project grade sum
float tsum;// test grade sum
float pave;// project grade average
float tave;// project grade average

    cout << "Let's calculate your grades!"<<endl;
    cout << "Enter your 6 program grades"<<endl; 

//this will allow you to input exactly 6 project grades 
for (int i=0; i < 6; i++) {
    cin >> proj[6];  }

//This will output the project grades
    cout << "\nThe project grades you entered were:" << endl;

for (int i=0; i < 6; i++)
    cout << proj[i] << endl;
// add the project grades together and then compute average
for (int i=0; i < 1; i++){
    psum += psum + proj[i];//this is supposed to add the values in the array

    cout<<endl;
    cout<<"this is sum"<<psum<<endl;//this is to test the sum

    pave=psum/6;
    cout<<pave<<endl;}//this is to test the average



    cout <<endl;  
    cout << "Great..................."<<endl;
    cout << "Now enter your 2 test grades"<<endl;




//this will allow you to input exactly 2 test grades
 for (int i=0; i < 2; i++) {
    cin >> test[i];  }

//This will output the test grades
    cout << "\nThe test grades you entered were:" << endl;

for (int i=0; i < 2; i++)
    cout << test[i] << endl;
// add the test grades together and then compute average
for (int i=0; i < 1; i++){
    tsum += tsum + test[i];//this is supposed to add the values in the array

    cout<<endl;
    cout<<"this is sum"<<tsum<<endl;//this is to test the sum

    tave=tsum/6;
    cout<<tave<<endl;}//this is to test the average

  return 0;
}

To sum an array with numElements number of elements, you can initialize the sum to 0, then loop through the array, adding each array element's value to the sum.

Assuming array is a[] and there are numElements elements in the array:

int sum = 0;
for (int i = 0; i < numElements; i++)
{
     sum = sum + a[i];
}

The average is simply the sum divided by the number of elements (be careful of integer division roundoff error. If this is a problem, define average to be a double rather than an int).

int average = sum / numElements;

This is probably too advanced right now, but the STL has functions for a lot of these common tasks built in. You can fill a vector--instead of an array--with an unknown number of grades using copy and a back_inserter, print the vector using copy, and get the sum of a list of grades using accumulate:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>

int main()
{
  std::vector<int> grades;

  std::cout << "Student grades: ";
  std::copy(std::istream_iterator<int>(std::cin),
    std::istream_iterator<int>(), std::back_inserter(grades));

  double sum = std::accumulate(grades.begin(), grades.end(), 0);

  std::cout << "Grades\n------\n";
  std::copy(grades.begin(), grades.end(),
    std::ostream_iterator<int>(std::cout, "\n"));
  std::cout << "Average: " << sum / grades.size() << '\n';
}

That takes a lot of the manual work out of the process.

Wow!! That was fast.. Thanks both of you. VernonDozier, where does numElements come from? Don't I need to define that or something?? Also, when I run the program and input and numbers and have the program spit them back out there not the same... Any advice on that?? Sorry still learning!! Really appreciate the help!

> where does numElements come from?
It represents the size of the array. You could use a literal value if you want:

int average = sum / 6;

> when I run the program and input and numbers and have the program spit them back out there not the same
You're writing to proj[6] for all of the numbers, and proj[6] is outside the bounds of the array. It looks like a typo and you meant to use proj instead:

for (int i=0; i < 6; i++) {
    cin >> proj[i]; }

You could also make it so you dont have to manually set the array size like this:
(Assuming your not dynamically allocating the array)

int nums[] = {324, 5462, 2};
int average = 0;

short arraySize = sizeof(nums) / sizeof(*nums);

for (short i = 0; i < arraySize; i++)
	average += nums[i];

average /= arraySize;

cout << average;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.