Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Fundamentals I COSC1436 Lab 7. Arrays Objectives: 1. to learn to use arrays with integer indexes. 2. to learn to use arrays with enum

Programming Fundamentals I

COSC1436

Lab 7. Arrays

Objectives:

1.to learn to use arrays with integer indexes.

2.to learn to use arrays with enum indexes.

3.to learn to multi-dimensional arrays.

Read Chapters 7 of your textbook.

Lab Submission Instructions

Answer the questions in each task in the space provided on this document. Make sure to copy and paste your code and a screen shot of your program execution for each task where it says demo your work. Submit this document on Canvas EOL.

Please indicate here which tasks you have successfully completed:

Task 1: One-dimensional arrays with integer indexes

Exercise 1.Create a new C++ project in Visual Studio, add a new C++ file and copy the following code into it:

// Program Reverse reads numbers into an array

// and prints them out in reverse order.

#include

#include

using namespace std;

const int MAX = 10;

int main ()

{

int numbers[MAX];

ifstream inData;

int value;

int index;

inData.open("reverse.txt");

for (index = 0; index < MAX; index++)

{

// FILL IN Code to read value from file inData

// and store it in array numbers at position indicated by variable index

}

for (index = MAX - 1; index >= 0; index--)

// FILL IN Code to write numbers on the screen

system(pause);

return 0;

}

Exercise 2.Complete the missing code under the highlighted lines and run the program with the sample data above.

Exercise 3.Extend the program to compute and display the sum of the values store in array numbers. What was displayed by the program for the above sample data?

Exercise 4.Paste a copy of your source code and a screen shot showing program output below

.

Task 2: One-dimensional arrays with enum indexes

Exercise 1.Create a new C++ project in Visual Studio, add a new C++ file and copy the following code into it:

// Program Favorit surveys users to determine the favorite soft drink.

#include

using namespace std;

enum Drinks {COKE, PEPSI, SPRITE, DR_PEPPER};

void Prompt();

int main ()

{

int favorites[4]; //holds sum of users who favor each drink

int number;

Drinks index;

for (index = COKE; index <= DR_PEPPER; index = Drinks(index+1))

// FILL IN Code to initialize array favorites to all zeros

Prompt();

cin >> number;

while (number != 4)

{

// FILL IN Code to increment the proper drink fvorites based on user selections

// e.g. if user enter 0 increment favorites[COKE] etc.

Prompt();

cin >> number;

}

// FILL IN THE Code to write out the totals in the format:

// Drink Number of favorites

system(pause);

return 0;

}

/*******************************************************/

void Prompt()

{

cout << "Enter a 0 if your favorite is a Coke." << endl;

cout << "Enter a 1 if your favorite is a Pepsi." << endl;

cout << "Enter a 2 if your favorite is a Sprite." << endl;

cout << "Enter a 3 if your favorite is a DrPepper." << endl;

cout <<"Enter a 4 if you wish to quit the survey." << endl;

}

Exercise 2.Complete the missing code under the highlighted lines. Run you program and demo it to your instructor.

Exercise 3.Add a function to take the array favorites as a parameter and display the percentage of responses each drink received. Run your program with sample data set and show the results.

Exercise 4.Paste a copy of your source code and a screen shot showing program output below

Task 3: Two-dimensional arrays

Exercise 1.Create a new C++ project in Visual Studio, add a new C++ file and copy the following code into it:

// Program TwoDim manipulates a two-dimensional array variable.

#include

#include

using namespace std;

const int ROW_MAX = 8;

const int COL_MAX = 10;

typedef int ItemType;

typedef ItemType Table[ROW_MAX][COL_MAX];

void getData(ifstream&, Table, int&, int&);

// Reads values and stores them in the table.

void printTable(ofstream&, const Table, int, int);

// Write values in the table to a file.

int main ()

{

Table table;

int rowsUsed;

int colsUsed;

ifstream dataIn;

ofstream dataOut;

dataIn.open("twod.txt");

dataOut.open("twod.out");

getData(dataIn, table, rowsUsed, colsUsed);

printTable(dataOut, table, rowsUsed, colsUsed);

return 0;

}

//***************************************************

void getData(ifstream& data, Table table, int& rowsUsed, int& colsUsed)

// Pre: rowsUsed and colsUsed are on the first line of

// file data; values are one row per line

// beginning with the second line.

// Post: Values have been read and stored in the table.

{

ItemType item;

data >> rowsUsed >> colsUsed;

for (int row = 0; row < rowsUsed; row++)

for (int col = 0; col < colsUsed; col++)

// FILL IN Code to read the next value from input file and store it in array table.

}

//****************************************************

void printTable(ofstream& data, const Table table,

int rowsUsed, int colsUsed)

// Pre: table contains valid data.

// Post: Values in array table have been sent to a file by row,

// one row per line.

{

// FILL IN Code to write values in array table in output file stream data one row per line.

}

Exercise 2.Read the documentation carefully and predict the output for the following input data in file twod.txt:

4 5

3 1 4 1 5

2 3 6 7 1

7 8 8 8 8

9 8 7 6 5

Exercise 3.Complete the missing code under the highlighted lines. Run completed program and show your results.

Exercise 4.Add a function that computes and returns the largest value in array table. Add a statement in function main() to call function and display the largest value in array table.

Exercise 5.Add a function that computes and returns the smallest value in array table. Add a statement in function main() to call function and display the smallest value in array table.

Exercise 6.Add a function that takes a one dimensional array as a parameter and its size and returns the sum of values. Use .this function to display the sum of values in each column in array table.

Exercise 7.Paste a copy of your source code and a screen shot showing program output below

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Administrator Limited Edition

Authors: Martif Way

1st Edition

B0CGG89N8Z

More Books

Students also viewed these Databases questions

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago