Lab 6 Solution

$29.99 $18.99

Introduction: So far the only input we’ve had for our programs has come from the console. But often times we will want to input so many values that it would be tedious to type them all out. Or we will want to use the same inputs over and over without having to retype them. In…

You’ll get a: . zip file solution

 

 
Categorys:
Tags:

Description

Rate this product

Introduction:

So far the only input we’ve had for our programs has come from the console. But often times we will want to input so many values that it would be tedious to type them all out. Or we will want to use the same inputs over and over without having to retype them. In these cases, we can take inputs from files rather than rather than from the console.

File I/O in C++ can be accomplished using the <fstream>library, which works much like the <iostream>library that you’re used to. Values are “extracted” from the file stream with the >> operator and and “inserted” into the stream with the << operator. The main difference is that we must explicitly open a file I/O stream unlike cout and cin which are already created for us.

The purpose of today’s lab is to introduce you to file operations and to integrate those operations with the techniques we’ve already learned so far.

Directions:

1- Launch Code::Blocks and start a new file. Name it your_last_name_lab6.cpp. Save lab6_data.txt from TRACS into the same directory as your program.

2-Include the standard header for this lab:

//Your Name

//CS1428

//Lab 6

3- Include the iostream and fstream standard libraries and start your main function:

#include <iostream>

#include <fstream>

using namespace std;

int main() {

4-You’ve been hired by a small business to do some simple data analysis. They’ve been counting the daily page views for their website and want to know how the numbers are trending. They have given you

a file with all of the numbers they’ve collected separated with a single blank space. The first number in this file is the number of entries in the set! Everything after is a data point that you need to analyze. Debug the following code written by their in-house developer to load the data into an array:

// This part is correct!

ifstream dataFile;

dataFile.open(“lab6_data.txt”);

int setSize = 0;

dataFile >> setSize;

//After this is broken!

int[setSize] data;

int counter;

while (conter <= setSize) {

dataFile << data[counter];

counter+;

}

5- Implement the following pseudo-code to start your analysis of your client’s data:

//1. DECLARE ANOTHER FILESTREAM TO HANDLE YOUR PROGRAMS OUTPUT //2. OPEN THE FILESTREAM FOR OUTPUT WITH THE FILENAME “lab6_output.txt” //3. DECLARE TWO VARIABLES CALLED maxViews AND minViews

//4. DECLARE ANOTHER VARIABLE TO TEMPORARILY HOLD VALUES FROM data //5. INITIALIZE minViews WITH A VERY LARGE VALUE. INITIALIZE maxViews // WITH A VERY SMALL VALUE

//6. ENTER A LOOP WHICH WILL COUNT THROUGH THE INDEXES OF data

//7. ASSIGN YOUR TEMP VAR TO EQUAL THE VALUE IN THIS INDEX OF //data

//8. IF THE VALUE IN data AT THIS INDEX IS GREATER THAN maxViews, //UPDATE maxViews

//9. IF THE VALUE IN data AT THIS INDEX IS SMALLER THAN minViews, //UPDATE minViews

//10. EXIT LOOP

//11. WRITE maxViews AND minViews INTO THE OUTPUT FILE WITH AN //APPROPRIATE LABEL

6-The final piece of you data analysis will be to apply a technique called Linear Regression. This means creating a line which we think will lay neatly over the data. To do this we will calculate 3 things:

1. The slope of this line using the equation: (y – y ) / ( x – x ). max min max min

2. The y-intercept of this line using the equation: y= y– slope.

0 1

  1. The sum of errors, which will tell us the sum of all the differences between our line and the actual data. On a well fit line this number will be near zero. Note: in an actual analysis, we would use the sum of squared errors rather than the sum of errors. Can you think of why?

Implement the following pseudo-code to perform your linear regression:

//1. DECLARE VARIABLES OF AN APPROPRIATE TYPE TO HOLD THE SLOPE, //Y-INTERCEPT, AND SUM OF ERRORS

//2. ASSIGN SLOPE VALUE EQUAL TO maxViews – minViews OVER setSize //3. USE THE SLOPE AND data[0] TO CALCULATE THE Y-INTERCEPT

//4. ENTER A LOOP WHICH WILL COUNT THROUGH ALL INDEXES OF data

//5. CALCULATE THE DIFFERENCE BETWEEN slope*i + y-intercept AND data[i]

//6. ADD THIS NUMBER INTO YOUR SUM OF ERRORS.

//5. END LOOP

//6. WRITE THE SLOPE, Y-INTERCEPT, AND SUM OF ERRORS INTO THE OUTPUT //FILE WITH APPROPRIATE LABELS.

  1. Close your file streams. Build and run your code. Fix and errors. Your output file should look something like this:

  1. Submit your .cpp file through TRACS. You do not need to attach the output file. You can leave when you’re done.