Lab 5

This week in lab we are going to be working on 3 things. First, we are going to use a real basic array to store numbers at a given index location. Second, we are going to create a simple struct. Finally, we are going to make an array of our struct and store values in it.

Part 1

For this part, you are going to create an array of ints of size 10. You will then make a function with the following signature:

void readInts( istream& in, ostream& out );

and this needs to be declared in a header called:

lab5.h

The function will read two numbers per line from the input. The first number represents an index location. The second number is the number we wish to store, if the index is valid, i.e. greater than or equal to 0 and less than 10.

Once all the data has been read in and possibly stored in the array, you will loop over the array and give me the index location and the value stored there.

Example Input

0 1
10 2
7 3
-2 4

Example Output

Index Value
0 1
1 ?
2 ?
3 ?
4 ?
5 ?
6 ?
7 3
8 ?
9 ?

You should not initialize your array and then print out ?'s Just print out the values in the array.

I used ? above to represent the value that I don't know. Since those locations didn't get a value C++ doesn't guarantee any value. I will check that the good values are set.

Testing

It's always a good idea to test your code. I'll provide a real simple main you can use.

#include <iostream>
#include "lab5.h"
using std::cout;
using std::cin;
using std::endl;

int main()
{
    readInts( cin, cout );
}

Submission of Part 1

To turn this in and get it scored, zip up your lab5.h and any implementation file you wrote for this and turn it in to Web-CAT.

Part 2

This being the second part of the lab, we will do the middle part now. For this part, we are going to make a basic struct. This struct will store a string and a double. The struct will be called

basicStruct

and it will be declared in a header file called:

lab5.h

I'd put this right above the function you declared in part 1.

The double needs to be called number and the string needs to be called name.

Testing

This part's a little harder to test, but here's some code that you can use that will at least compile it and see if it works.

#include <iostream>
#include "lab5.h"
using std::cout;
using std::cin;
using std::endl;

int main()
{
    struct basic;
    basic.number = 10.5;
    basic.name = "this is my name";
    cout << "The number is: " << basic.number << endl;//this should say "The number is: 10.5"
    cout << "The name is: " << basic.name << endl; //this should say, "The name is: this is my name"

}

Submission of Part 2

All you need to do for this part, is turn in your lab5.h file. You don't need to zip it and you don't need the implementation. For me to test this, all I need is the declaration of the struct.

Part III

This is the last part and by now you are probably exhausted from all the typing. This will put parts 1 and 2 together and make an array of structs (sounds like a project idea).

You are going to write one last function called:

void readData( istream& in, ostream& );

declare it in, you guessed it:

lab5.h

I would make it the last declaration in the file. So have the struct first, then part 1's declaration and then finally this one.

This function will work more or less like the part 1. It will read data and store it in the array. The array this time is an array of the basicStruct type instead of ints, so our input will be an index, a string and a double. You are to read the index location, the string and the double. If the index is good, then store the string and the double in the array at the given location.

Sample Input

0 Dave 10.3 
1 Kelly 15.2 
-1 Sue 45.5 
10 Michael 36.5 
2 Matt 23.1 
4 Molly 7.25

Sample Output

Index Name Number 
0 Dave 10.3 
1 Kelly 15.2 
2 Matt 23.1 
3 ? ? 
4 Molly 7.25 
5 ? ? 
6 ? ? 
7 ? ? 
8 ? ? 
9 ? ?

You should not initialize your array and then print out ?'s Just print out the values in the array.

I used ? above to represent the value that I don't know. Since those locations didn't get a value C++ doesn't guarantee any value. I will check that the good values are set.

Testing

It's always a good idea to test your code. I'll provide a real simple main you can use.

#include <iostream>
#include "lab5.h"
using std::cout;
using std::cin;
using std::endl;

int main()
{
    readData( cin, cout );
}

The output here will depend on what you type in on the keyboard. I would enter some valid data and some invalid data. Pressing control+Z will get the loop to stop.

Submission of Part III

For this submission, zip up your lab5.h file and the implementation file. You can put all the code for both parts 1 and III in the same cpp that is fine.