Thread: I need help with two parallel arrays

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    I need help with two parallel arrays

    I'm suppose to write a program that uses two parallel arrays to store student names and their grades. It should use an array of strings that hold the names and and array of integers that hold the grades. The program should produce a report that displays list of students names and grades, and the name of the student that has the highest grade. The names and grades should be stored using an initialization list at the time the arrays are created. Use a loop and conditional statement to determine which student has the highest grade.

    This is what I have tried so far. I'm new at this and there not many good examples in the book


    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int i = 0, h = 0, saved = 0;
    
    	int g[ ] = {87, 99, 70, 75, 77, 91, 95};
    	string s [ ]= {"S1", "S2", "S3", "S4", "S5", "S6", "S7"};
    
    	while ( i <= 3)
    	{if (g[i] > h)
    	{h = g[i];
    	  saved = i;
    	}
    	}
    	cout << "Highest " << h;
    	cout << "student is " << s[saved];
    
    	return 0;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while ( i <= 3)
    You need a for loop, to examine each element of the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    while ( i <= 3)
    where do you get this 3 stuff?

    in general loop for all array members will look like

    Code:
    for(i=0; i < sizeof(g)/sizeof(g[0]); i++)
    {
    }
    after that - your finding highest score alg should work fine

    and what left - is to use another loop to print all students (or even the same loop)
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Parallel Arrays
    By kippwinger in forum C++ Programming
    Replies: 3
    Last Post: 06-26-2003, 03:18 PM
  3. Parallel Arrays-HELP!
    By ProgrammingDlux in forum C++ Programming
    Replies: 3
    Last Post: 03-28-2002, 03:55 PM
  4. Parallel Arrays with Multiple Arrays
    By Billye Scott in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 11:14 PM
  5. parallel arrays
    By KeeNCPP in forum C++ Programming
    Replies: 1
    Last Post: 10-18-2001, 09:56 PM