CS 367-002 Spring 2010
Homework 1 - A Short C Refresher
Due: Thursday, January 28, class time
Late homework will be penalized 25% per day
Instructions
The purpose of this assignment is to review your knowledge of the C programming language as it relates to simple array data types, dynamic memory allocation, parameter passing in functions and procedures (address vs. pointers), and file I/O. These are all concepts you will need to know for the course.
- You may work in groups of size two. Turn in one copy of the assignment for the group, with each group member’s name on it.
- Submit a hardcopy of:
- Your *commented* C source code.
- A Statement of Correctness in which you either state that your program is (believed to be) correct or you describe any problems that you are aware of.
- Specified output from your program when run using the data.txt data set.
Assignment Specifications
Write a C program that reads floating point values from a file and determines the average, largest and second largest of the values in the data set. The number of elements of the data set is defined by the first value in the file. Here's an outline of the algorithm to be used:
- In main(), open the input file data.txt. NOTE - be sure your program would work correctly on any given data file, not just this one.
- Read n: the first line of the input file is the number n of values in the file.
- Create a dynamic array large enough to hold n floating point values. Note: different input files require different sized arrays depending on the size of the data set.
- Read the n floating point values from the file into the array.
- Find the average of the n floating point values in the array.
- Find the largest and second largest floats in the array.
- Output all of the float values in the array. Then output the average,
the largest and the second largest float values.
Additional Code Requirements
Your program must include the following functions:
- The main() function opens the input file, reads the first line, and calls the functions readNumbers, Average and LargeNumbers. Then it prints the average, largest and second largest values returned from the Average and LargeNumbers functions, respectively.
- void readNumbers(...) which reads the n floating point values from the input file. It must have three parameters:
- the array
- the input file (already opened in main())
- the number n (which was already read by main()
- float Average(...) calculates the average of the values in the array. It must have two parameters:
- the array
- the number n (which was read by main())
It must return value the average as a float value.
- void LargeNumbers(...) which computes the largest and second largest values. It must have
four parameters:
- the array
- number n of floating point values in the file (which was read by main()
- the largest float
- the second largest float
The last two parameters are used to “return” the largest and second largest floating point values of the array. Recall that C passes function parameters by value- in order to return the two numbers that are required here, you will need to use addresses and pointers.
Notes
Here are some code segments that you may find useful:
Header files to include:
- #include // for IO
- #include // for "return EXIT_SUCCESS;" at the end of main()
//Read formatted data from stdin using scanf
#include
int main () {
char str[80];
int i;
printf ("Enter your last name: ");
scanf ("%s",str);
printf ("Enter your age: ");
scanf ("%d",&i);
printf ("Last Name = %s , age = %d .\n",str,i);
printf ("Enter a hexadecimal number: ");
scanf ("%x",&i);
printf ("You have entered %#x (%d).\n",i,i);
return 0;
}
//Read and write a floating point value from and to the console
#include
int main(void) {
float fl;
scanf("%f", &fl);
printf("%f", fl);
printf("\n");
printf("%.2f\n",fl); /* round to nearest hundredths place */
return 0;
}
// Demonstrates how to read a number
#include
char line[100];
int value;
int main() {
printf("Enter a value: ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%d", &value);
printf("Twice %d is %d\n", value, value * 2);
return (0); // or: return EXIT_SUCCESS;
}
// Defining a dynamic array of n ints:
int *dynamicArray;
int n = 10;
dynamicArray = (int *) malloc (n * sizeof(int));
// How to write data to a file and read it back
#include
int main () {
char str [80];
float f;
FILE * pFile;
pFile = fopen ("myfile.txt","w+");
fprintf (pFile, "%f %s", 3.1416, "PI"); // print to file myfile.txt
rewind (pFile);
fscanf (pFile, "%f %s", &f, str); // read from file myfile.txt
fclose (pFile);
printf ("I have read: %f and %s \n",f,str);
return 0;
}
// Demonstrates how to read unknown number of multiple lines from a file
#include
int main() {
FILE *file;
int numbers[30];
/* make sure numbers is large enough to hold all the data. */
int i,j;
file = fopen("fscanf.txt", "r");
if(file==NULL) {
printf("Error: can't open file.\n");
return 1;
}
else {
printf("File opened successfully.\n");
i = 0 ;
/* loop through and store the numbers into the array */
while(fscanf(file, "%d", &numbers[i])!= EOF) {
i++;
}
printf("Number of numbers read: %d\n\n", i);
printf("The numbers are:\n");
for(j=0 ; j<i ; j++) { /* print numbers 1 by 1 */
printf("%d\n", numbers[j]);
}
fclose(file);
return 0;
}
}
Additional Help
There are several links to C Tutorials on our class website. There are also some slides on C programming available on our class website- Lecture Timetable webpage called “Something about C”. This reviews some major concepts in C before discussing makefiles and debuggers. A printf reference can be found at:
http://www.cplusplus.com/reference/clibrary/cstdio/printf.html
If you complete this assignment using software on your laptop, then you must also try running your code on the ite.gmu.edu Linux cluster to make sure you understand how to use basic Unix commands and how the gcc compiler works on that system. Our UTAs can help with this if you need assistance.