Presentation is loading. Please wait.

Presentation is loading. Please wait.

ecec201 – advanced programming for engineers

Similar presentations


Presentation on theme: "ecec201 – advanced programming for engineers"— Presentation transcript:

1 ecec201 – advanced programming for engineers
problem set 3 Revised with additional slide (end) giving hints on copying csv file from server to laptop via ssh

2 overview write a program that generates some number of random values in the range of [0,1] and writes those to a comma separated value (CSV) file. the program should output the minimum, maximum and mean value of the generated random numbers to the command line. the program takes as input from the command line (1) an output file and (2) a number indicating how many random numbers to generate. validate your program by (1) tracing in the debugger and (2) opening the output file in Excel, computing the min,max and mean and verifying they match the program output.

3 approach the textbook is the only resource you may use
before you set out to code, think through the basic outline of tasks you will need to implement for your program Three new topics are introduced – (1) arguments to main, (2) for loops and (3) file i/o. hints are provided to help introduce these. answer all questions and use screen shots for verify / validate steps your screen shots should be large and easy to read and should not contain extra text. don’t make the grader hunt for the correct answer add a caption to the screenshot to make it more clear

4 p0 – arguments to main in a new c program, use the following template for main int main(int argc, char ** argv) { Question 0: what is the value of argc when you run the program with no inputs? print the value of argv[0]. You can treat this as a string for the printf format statement. use the ‘watch’ debug window to add a watch on the variable of argv[0]. Question 1: what is the first argument passed to a c program?

5 passing arguments from cli
modify your program to check the appropriate arguments if (3!=argc) { printf(“USAGE : genRand <#ofRands> <filename>”); return -1; } run your program from the terminal and verify this works note that from the terminal, use ‘./genRand …’ to run your program. bash does not put the current folder on the path by default, so just typing ‘genRand’ won’t find it. modify your program to print the values of argv[1] and argv[2] (again as strings) run your program from the terminal, passing test arguments, e.g. ‘genRand 5 randOutput.csv’ validate that it prints the arguments correctly

6 passing arguments into the debugger
in your launch.json, find the entry for “args”. set your array of arguments (note this is javascript format) to be [“5”,”randOutput.csv”] note that vscode (lint) should indicate with colored strips if you have a syntax error run your program in the debugger (f5). verify the arguments are passed correctly note that both program arguments (“5” and “randOutput.csv”) are passed as string variables (the char ** argv is an array of strings – more on that later!). you can convert strings to their numeric value using the atoi function. look up atoi in the textbook standard library appendix. Question 2: what other string to numeric functions are there in the standard library?

7 you now have the output file name (argv[2]) and the number of random #’s to generate (argv[1]). let’s generate some random #’s look up the rand function in the textbook standard library appendix. Question 3: what is the range of the rand function? how would you generate a random number between 0 and 1? hint: you can ‘cast’ the output of rand to be a float, and then normalize it: float fRand=(float)rand()/[?]; // [?] is part of the answer to Q3… to generate nRandom random #’s, use a for loop: for (var i=0;i<nRandom;i++) { fRand=…; // generate random number on [0,1] printf(“…”); // print out generated random # } make your program print out nRandom random #’s, where nRandom is the value you passed in via the command line run your program 5 times, printing out 5 values each time. Question 4: what is unusual about your random output?

8 seeding the random # generator
look up the srand function in the textbook look up the time function in the textbook what header file is it defined in? use srand(time(0)) at the beginning of your program to seed the random number generator. rerun your program as above, and validate the output

9 compute the min,max and mean
each time you generate a random #, check it against min and max using an if statement: if (fRand<frandMin) frandMin=frand; if (fRand>frandMax) frandMax=fRand; verify your min and max values using the debugger Question 5: how should frandMin and frandMax be initialized? Question 6: how do you implement the mean? hint: typos (if this helps, thank Claire and Karen)

10 output #’s to a file read the text section on ‘fopen’ (pp. 543-545)
what mode will we need to open our output file in? I suggest you name the output argument with a ‘.csv’ extension be sure to check the return value from fopen and print an error if it fails. validate this. Question 7: how can you force fopen to fail? instead of writing your random values to the console, write them to the .csv file. you should output to the console the number of values written and the filename you wrote to, along with the min,max and mean values extra credit: modify your program so it does not output a comma after the last value written verify

11 open your file in excel, and validate the results
use Microsoft excel to import your csv file compute the min,max and mean in excel, and display those on your spreadsheet validate that these values match the values output by the program

12 Hint – copy file from server to laptop
If your server ip is e.g And your server username is steve And your file on the server is located at /home/steve/ecec201/ps3/randOutput.csv (hint use ‘pwd’ to see the full path) THEN copy from laptop to server by typing the following in a terminal on the laptop: scp . This will copy the file randOutput.csv from the server to the current folder in the laptop.


Download ppt "ecec201 – advanced programming for engineers"

Similar presentations


Ads by Google