CSC270 Survey of Programming Languages In Class

C Programming Exercises

Exercise Description Comment

Practice Reference Pass

Practice passing by reference. here is the working exercise:  
Create a Function Using a Static Variable Create a program that has a main method and a function to calculate a price. The list price always gets a 5% discount. Every third price requested gets an extra 5% discount.  
Practice Reference pass and Arrays directions to print an array of dice and return both the sum and the highest number - have to use a reference (because we did not learn about structures)  
Array size Just look at array size and how the function loses the size here is the working exercise  

ArrayDemo.c

DynArrayDemo.c

PtrDemo.c

MultArrayDemo.c

See demo programs for dynamic arrays and make changes

Here is the DynArrayDemo.c working. Note that while the array variable a holds an address, the cell variable a[4] does not hold an address. Therefore, when you pass the array variable to scanf, just pass a, but when you pass the cell variable a[index] to scanf, you must pass &a[index].

 

StringFunctions1.c

StringFunctions2.c

See demo programs for strings and make changes

wget http://home.adelphi.edu/~pe16132/csc270/note/StringSampleCode/StringFunctions1.c

wget http://home.adelphi.edu/~pe16132/csc270/note/StringSampleCode/StringFunctions2.c

 

stringFunctions3.c

StringFunctions3ToDebug.c

Make requested changes inside stringFunctions3.c

wget http://home.adelphi.edu/~pe16132/csc270/note/StringSampleCode/stringFunctions3.c

 
avggrade.c

wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/avggrade.c

Make these changes:
// in your main method, make a variable for a student2 and
// read and write that student's info as well
// then print the name of the student with the higher average
// IF TIME:
// add a place for the student's major in the exam structure
// then read it in and write it out

 

 
bitwise.c

wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/bitwise.c

Trace this before running it and predict the numbers that will print

Make these changes:
//Pick two numbers to apply a bitwise indicator to, apply the bitwise indicator and print

 
pointertrace.txt

wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/pointertrace.c

Answer - run and compile: pointertrace.c

 

very simple read and write text

  • fgets
  • sscanf
  • fprintf

wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/simplewriter.c

  • reads file1.txt and writes file2.txt as a text file
  • sample file1.txt: wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/file1.txt
 

simple read a text file and write a binary file

  • fgets
  • fwrite

wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/simplewriterb.c

  • reads file1.txt and writes file2.txt as a binary file
  • sample file1.txt: wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/file1.txt
 

simple read a binary file and write a new binary file

  • fread and fwrite

wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/simplereaderb.c

  • reads file2.txt and writes file3.txt
  • sample file2.txt: wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/file2.txt
 

update a binary file

  • fseek to position
  • fwrite

wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/simpleupdaterb.c

  • reads file3.txt and updates the first and third record
  • sample file3.txt: wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/file3.txt
  • view results with od -d file3.txt

 

 
     

text file read and write

  • get arguments from command line (argv)
  • fopen, getc, putc, fclose
  • tolower

 

text file read and write

wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/tolower2Working.c

--- sample input file: wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/input1.txt

Non-working exercise: wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/tolower2.c

 
     
Read a text file / write a binary and then reverse

 

wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/writerecWorking.c

  • Read text / Write Binary:
  • read text file using getc; write binary file using fwrite; use sscanf to parse a buffer (note that fgets is a reasonable alternative to getc as we used it here)

wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/readrecWorking.c

  • read Binary / write Text:
  • read a binary file using fread; write to a text file using fprintf

wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/rec_bin.txt

  • binary file you can use as input to readrecWorking.c

wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/rec_text.txt

  • text file you can use as input to writerecWorking.c

Non-Working versions of the binary read and write for practice:

  • wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/writerec.c
  • wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/readrec.c

 

 

Make file demo

makefile including one c and h file only

wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/simplemake.c

wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/simplemake.h

wget http://home.adelphi.edu/~pe16132/csc270/note/SampleCode/Makefile