Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 20th, 2007, 04:09 PM
Authorized User
 
Join Date: Oct 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mastrgamr
Default array hlp

#include <ostream.h>
using namespace std;

int main() {
    int grades[5], avg, x;

    cout<<" Type in your 4 main grades: \n\n";
    cout<<"Social Studies: ";
    cin >> grades[0];
    cout<<"Math: ";
    cin >> grades[1];
    cout<<"Science: ";
    cin >> grades[2];
    cout<<"English: ";
    cin >> grades[3];

    // average them all
    avg=0;
    for(x=1; x<5; x++)
      avg += grades[x];
      avg /= 5;

    cout<<"Your Average is: "<< avg<<'\n';

    cin.get();
    return 0;
}


is there somthin wrong with this or is it that u cannot add vales to arrays?? i want the final outcome to show the average but instead it shows 502328282 y?

Mŧ~|~®GÃ(v)R~
__________________
-Stuart Smith
Reply With Quote
  #2 (permalink)  
Old January 21st, 2007, 10:00 PM
Registered User
 
Join Date: Jan 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

what i can see is when u cin the value start from first element in the array, which is grades[0], but when in the for loop u sum up the value start from the second element which is grades[1]..

Reply With Quote
  #3 (permalink)  
Old January 22nd, 2007, 09:24 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

Your problem is not the starting of the for loop but it is the ending of the for loop

in your for loop it is correct that you start at one and if you wanted the begining of the array you would start at zero however at the end you state less than five which would mean it stops at four your array however is assigned 4 values that would be grades[0-3] which means you must state that the loop stops at less than four so that the unnassigned variable isnt used thats why your average is all messed up

~ Geo

~ Don't take life too seriously, you'll never get out alive!
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Go from 2d Array to 1d array without defining type OneQuestion General .NET 1 January 10th, 2008 11:13 AM
code to read in Access tables to a dataset - hlp! ruairi ADO.NET 4 February 12th, 2005 06:03 PM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM
pls hlp!!! Microsoft ADO Data Control not found x2c4u VB Databases Basics 3 September 29th, 2004 09:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.