LAB 01: Standard Deviation Lab
Objective
Learning C programming, C array processing. Topics include
- Compiling
C programs
- keyboard I/O:printf() and scanf() (C)
cout/cin
(C++),
- file creation and file i/o: FILE
*infile, FILE *outfile, and fopen() (C)
fscanf() and fprintf() (C)
ifstream
infile() and ofstream outfile() (C++),
- Constants: #define MAXVALS 100 (C)
const int MAXVALS=100; (C++)
- static array: int myarray[MAXVALS]:
C
and
C++
- dynamic arrays:
C:
int *myarray;
scanf("%d", &numVals); myarray=(int *)malloc(sizeof(int)*numVals);
"malloc" stands for "memory allocation"
- deletion of dynamic arrays
delete myarray (C++)
free(myarray) (C)
- dynamic arrays:
C++:
int *myarray;
cin >> numVals; myarray=new int[numVals];
- "Random" numbers in
C
and
C++
Part 1
- Download these programs to write random numbers to a file:
randomNumFile.c,
(for C++ see
randomNumFile.cpp)
right click on the C file, choose "Download Link", save as .c (for c++ use
.cpp)
- Compile each program
C: gcc -o randomNumFile randomNumFile.c
(C++: g++ -o randomNumFileCPP randomNumFile.cpp)
- To create a generic executable name, use:
gcc randomNumfile.c
This makes an executable named a.out
- Run each program to create files of 50 or more random numbers
./randomNumFile (or ./a.out)
- Look at the contents of each of the number files to verify the programs
worked.
Part 2 - Static and Dynamic Arrays in C
- Write a C program to put 1000 random numbers in an array.
- Print the array of values
- Find the highest and lowest values
- Find the average value
- Find the standard deviation
- Sort the numbers