CS 2301/03 – Spring 2010 - Kennesaw State University



CSE 1321L: Programming and Problem Solving I Lab

Lab 12

Single-Dimensional Arrays

Note for C++ students: You may be wondering why the C++ students have a separate lab. Well, you’re likely an engineer of some kind, and working with files is super important (others learn this in CSE 1322).

In addition to working with arrays, you’re going to work with “fstream” – or a file stream. It works exactly the same way as cin and cout, you just have to set it up. It’s a super-convenient way to read and write files. The file type you’re going to write is a “.csv” file – or “comma-separated version”, which opens in Excel. The syntax for working with this is in the appendix.

Finally, we’re also going to show you a different way to create an array. The new syntax enables you to create an array that is dynamically sized instead of fixed when you compile the code. For example, if we asked you to create an array of 100 integers, you’d say:

int myArray[100];

However, if we asked you to base the size of the array off of a variable, then “int myArray[myNum];” wouldn’t work. The syntax will look like this:

int* myArray = new int[myNum];

where myNum is a variable of type integer. The only thing you need to remember to do is delete the array when you do it like this, which is a simple call:

delete(myArray);

Good luck on the lab, and don’t forget to look at the appendix!

Exercise #1: Design and implement a program (name it AssignGrades) that stores and processes numeric scores for a class. The program prompts the users to enter the class size (number of students) to create a single-dimensional array of that size to store the scores. The program prompts the user to enter a valid integer score (between 0 and 100) for each student. The program validates entered scores, rejects invalid scores, and stores only valid scores in the array.

The program defines method printGrades() that takes a single-dimensional array of integer scores as a parameter and processes the scores to print letter grades based on the following scale:

Grade is A if score >= 90 and score = 80 and score = 70 and score = 60 and score ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download