Lab 4

Part 1: Command-Line Arguments

Consider the following small program
#include <iostream>
using namespace std;

int main(int argc, char * argv[])
{
for (int i = 1; i<argc; i++)
cout << argv[i] << ' ';
cout << endl;
}

Save this program as argument.cpp and compile it. If we run the program:
$ ./a.out eenie meenie miney moe
eenie meenie miney moe
$

we see that this program echoes the command-line arguments. We have a main function header of the form int main(int argc, char * argv[]); argc is the number of terms in the command-line, including the command name, and argv[] is an array of strings which contains the command name in argv[0], and then all the command-line arguments in argv[1] through argv[argc-1]. The arguments are delimited by blanks and, so, no spaces will appear in any of the arguments passed into the program.

Assignment:

Modify argument.cpp so that it converts the arguments to integers, computes their sum, and then prints out this value.  For example,

$ ./a.out 3 6 10
Sum is: 19
$

Hint: Look Here

Part 2: Practice with structs, arrays and sorting

Consider the following struct

struct Student
{
   int StudentID;
   int ExamAverage;
}

Fill in the body for the following functions 

Student createNewStudent()
{
   static int StudentID = 0;
   //increment StudentID
   //Create new student object
   //Assign Student ID to the object
   //Generate a random test average between 50-100 and assign it
   //return student object 
}

void PrintArray( Student[] ClassRoster, int size )
{
    //Should iterate throught the array and print out the class
    //roster in a nice, readable format
}

void SortArray( Student[] ClassRoster, int size )
{
    //Should sort the array from highest grade to lowest
}

After you fill these functions in, create your main to create an array of 50 students, print out the unsorted array, sort it, and print it out again. You main should be VERY short.



Grading:
  1. 30% for Part 1
  2. 70% for Part 2