Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This lab assignment is designed to give you practice working with arrays. Work w

ID: 3709838 • Letter: T

Question

This lab assignment is designed to give you practice working with arrays. Work with a partner to create a program to read in and process data from the "Best BG Pizza" contest. Each partner can take a turn typing in a function but both partners should contribute to solving, testing and debugging each function.

Students were asked to "vote" for their favorite pizza place by buying pizza from that location on a particular weekend. The data file lab12.txt contains the number of pizzas sold that weekend by six pizza stores in BG. There are a total of 12 values in the file. The first six numbers are the number sold on Friday for each pizza place; the next six numbers are the number sold on Saturday for each place.

1.   First create a new Visual Studio C++ project and a .cpp file with a main function. In the main function define a constant for the size declarator with the value 12. Use the constant to declare an array to hold the pizza data. Note: One array should be used to hold all 12 values.

For each task below, add a function prototype before the main function, a function call in the main function and the function definition after the main function. Typically, the array and its size are passed as arguments to each function but you may have other arguments and return values. Make sure each function is working before you go on to the next one. Tip: Arrays are passed by reference by default but no ampersand (&) is necessary.

2. Copy the data file lab12.txt to your project folder. Then add a function to read the data from the file and store it in your array. The code in the function should open the file, test the success of the open, read in the data from this file and store it in the array and close the file.

Note: The remaining tasks will use the data stored in the array.

3. Write a function to display the value stored in each element of the array using a loop. Your output should look something like this:

  

             BG's Best Pizza

                Your names

         Element      Pizzas Sold

            0               25

            1               40

            2               20

         etc.            etc.

4. Write a function to find and return the total number of pizzas sold overall by the pizza stores for the weekend using a loop to find the sum of all the values in the array. The main function should display the result with a descriptive label. Make sure your total is correct.

* To earn the points for each function you must use a loop correctly to do the task, not separate assignment statements.

Bonus 1: (1 pt) Write a function to find and return the total number of pizzas sold on Friday by the six pizza stores. Note that the values for Friday's sales are stored in the first six elements of the array. The main function should display the result with a descriptive label. Make sure the total is correct. Then call the function a second time to find and return the total number of pizzas sold on Saturday by the six pizza stores. Note that these are the values stored in the last six elements of the array.

Discuss with your partner how you might change the function to be able to find either Friday or Saturday's total. Hint: One way would be to add another argument to the function to pass the starting position in the array for the six values to be totaled.

Bonus 2: (1 pt) Write a function to find the number of pizzas sold by each store over the weekend. For example, the number of pizzas sold by Store 1 on Friday is the value stored in the first array element and its number sold on Saturday is the value stored in the seventh array element so its total sales would be the sum of these two values. Store 2's values are stored in the second and eighth elements and so on. Figure out how to add the values in the correct pair of array elements for each store and print out a table like this:

         Store number   Pizzas sold on weekend

               1                    55

               2                    85

              etc.                 etc.

Bonus 3: (1 pt) Write a recursive function to display the value stored in each element of the array. See slide 19-18 for one way to do this. Call this function right after your original display function and check that the output is the same.

CS2010 Lab 12: Grading Rubric

.cpp file turned in on Canvas, named with your last names.

2          main function correctly reads in pizza data from file and stores it in array.

1.5       Function correctly displays pizza data stored in the array. Function prototype, call and definition correct, parameters passed by value/by reference as needed.

1.5       Function correctly finds and returns total number of pizzas sold for the weekend by all stores. Function prototype, call and definition correct, parameters passed by value/by reference as needed.

            Bonus 1! (1 pt) Function correctly finds and returns total number of pizzas sold on Friday/Saturday calling same function twice to get either Friday or Saturday's total.

            Bonus 2! (1 pt) Function correctly finds and displays total number of pizzas sold by each store.

            Bonus 3! (1 pt) Function recursively display array contents.

---------------------------------------------

5          Total Points (8 with bonuses)

lab12.txt contents:

25

40

20

55

15

20

30

45

20

40

25

30

sample

Fri Sat Total

25   30   55

40   45   85

20   20   40

55   40   95

15   25   40

20   30   50

175 190 365

Explanation / Answer

PLEASE REFER BELOW ANSWER

#include<iostream>

#include<fstream>

using namespace std;

//function prototype

void read_data(int*,const int);

void print_data(int*,const int);

int total_pizza_sold_weekend(int*,const int);

int main()

{

const int arr_size = 12; // size of the array

int arr[arr_size] ={0}; //array initialized to 0

int total_pizza_sold;

read_data(arr,arr_size); //function call to reading data from file

print_data(arr,arr_size); //function call to print data of file

total_pizza_sold = total_pizza_sold_weekend(arr,arr_size); //calculate total pizza sold on weekend

cout<<"total number of pizzas sold overall by the pizza stores for the weekend = "<<total_pizza_sold<<endl;

getchar();

//return 0;

}

void read_data(int *arr, const int arr_size)

{

ifstream infile; //input stream for file

infile.open("lab12.txt"); //reading file into stream

int i = 0;

if(infile) //if file is present then read into array line by line else exit();

{

while(infile >> arr[i])

{

if(i >= arr_size)

break;

i++;

}

}

else

{

cout<<"Unable to open file"<<endl;

return;

}

infile.close();

}

void print_data(int *arr, const int arr_size)

{

cout<<"BG's Best Pizza"<<endl;

cout<<"Your names"<<endl;

//printing array data

cout<<"Element Pizzas Sold"<<endl;

for(int i = 0; i < arr_size; i++)

{

cout<<i<<" "<<arr[i]<<endl;

}

}

int total_pizza_sold_weekend(int *arr,const int arr_size)

{

int sum = 0;

//finding sum of each element of the array

for(int i = 0; i < arr_size; i++)

sum += arr[i];

return sum;

}

lab12.txt CONTAINS

25
40
20
55
15
20
30
45
20
40
25
30

PLEASE REFER BELOW OUTPUT

BG's Best Pizza
Your names
Element Pizzas Sold
0 25
1 40
2 20
3 55
4 15
5 20
6 30
7 45
8 20
9 40
10 25
11 30
total number of pizzas sold overall by the pizza stores for the weekend = 365

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote