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.

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:

Additional Code Requirements

Your program must include the following functions:

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.