Exam #1 February 23rd (Next Friday)

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
C Slides and captured lecture (video and sound) are available at:
C Programming Tutorial – Part I CS Introduction to Operating Systems.
#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
 Complex data types  Structures  Defined types  Structures and functions  Structures and pointers.
1 CSC2100B Data Structure Tutorial 1 Online Judge and C.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Lecture 3: Getting Started & Input / Output (I/O)
LINKED LISTS.
Lesson #8 Structures Linked Lists Command Line Arguments.
User-Written Functions
Function: Declaration
Chapter 8 Arrays, Strings and Pointers
A bit of C programming Lecture 3 Uli Raich.
CS1010 Programming Methodology
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
C Programming Tutorial – Part I
Command Line Arguments
ECE Application Programming
Functions Separate Compilation
Visit for more Learning Resources
C Programming:Part 3 Characters and Strings File Processing Exercise.
EECE.2160 ECE Application Programming
Buy book Online -
LINKED LISTS.
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
CS 2308 Final Exam Review.
CS 2308 Exam II Review.
Pass by Reference, const, readonly, struct
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
CS 2308 Exam I Review.
Programming in C Pointer Basics.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
File Review Declare the File Stream Object Name
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Programming in C Pointer Basics.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
EECE.2160 ECE Application Programming
EE 312 Exam I Review.
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
C By Example The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass.
EECE.2160 ECE Application Programming
Arrays.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
C Programming - Lecture 5
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Programming in C Pointer Basics.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
EECE.2160 ECE Application Programming
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
EE 312 Exam I Review.
EECE.2160 ECE Application Programming
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
EECE.2160 ECE Application Programming
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Administrative things
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
EE 312 Exam I Review.
Presentation transcript:

Exam #1 February 23rd (Next Friday) input (scanf) and output (printf) format strings, and associated data types control structures - loops, conditionals function definition, parameters (pass by value and pass by reference), calling functions arrays recursion creating and using struct, typedef tracing C code to determine output and for identifying errors in code Note: code examples will be similar to assignments and exercises covered so far this semester

How do we pass structures into functions? We have the same two ways to pass structures into functions: pass by value - creates a copy of the entire structure that only the function can access - changes don’t affect the original pass by reference – just pass a pointer to the structure so that the function accesses the original structure Requires that we get a pointer to the structure so we can pass by reference

struct BOOK typedef struct { STRING title; STRING author; double cost; typedef char STRING[32]; typedef struct { STRING title; STRING author; double cost; } BOOK; BOOK my_book;

Pass a Structure by Value Assume we have a variable in main of type BOOK: BOOK my_book; Function header with a call-by-value parameter: void print_book(BOOK a_book); Calling the function: (passes a copy of the structure) print_book(my_book); Fields in the function are accessed in the same way as we would in main, using the dot: printf(“Title: %s\n”, a_book.title);

Pass a Structure by Reference Assume we have a variable in main of type BOOK: BOOK my_book; In main we create a pointer variable for a BOOK: BOOK *book_ptr; Next, we make it point to my_book: book_ptr = &my_book; Function header with a call-by-reference parameter: void print_book(BOOK *ptr); Calling the function: print_book(book_ptr); or print_book(&my_book);)

How do we access the fields? We have to use the -> operator to access fields when we pass by reference, not the dot operator: Assume we have called the function with: print_book(book_ptr); We access fields like this: printf(“Title: %s\n”, ptr->title); The -> says to “follow the pointer to the field” it works the same for numbers: printf("Cost: %5.2f\n", ptr->cost);

Putting it all together: typedef char STRING[255]; typedef struct { STRING title; STRING author; double cost; } BOOK; BOOK my_book; /* declare a BOOK variable */ BOOK *book_ptr; /* declare a pointer to a BOOK */ /* pass the BOOK variable by value */ void print_by_value(BOOK my_book); /* pass a pointer to a BOOK variable */ void print_by_reference(BOOK *ptr);

An Example Recall the following typedefs: typedef char STRING[32]; typedef struct { STRING course_name; double quiz_grades[5]; double average; } GRADE; STRING student_name; GRADE courses[10]; double gpa; } STUDENT_REC; STUDENT_REC students[100]; students array students[i] STUDENT_REC students[i].student_name string students[i].gpa double students[i].courses array students[i].courses[j] GRADE students[i].courses[j].course_name string students[i].courses[j].average double students[i].courses[j].quiz_grades array students[i].courses[j].quiz_grades[k] double

Problem Description Give a program that will input data for students from a file into an array of STUDENT_REC’s. Calculate the average for each class at student takes, and output the results (including all input data). Assume that there are 100 students, each student took 3 courses, and each course had 5 quizzes. Sample input file: smith 3.4 cse1001 94.2 97.0 96.4 93.9 100.0 mth2020 88.2 85.6 85.0 84.2 89.5 chm1400 75.6 72.3 77.0 76.1 70.2 jones 2.75 psy1201 74.2 77.0 76.4 73.9 70.0 aero2201 98.2 95.6 95.0 94.2 99.5 ece1040 85.6 82.3 87.0 86.1 80.2 :

Without Functions #include <stdio.h> typedef char STRING[32]; typedef struct { STRING course_name; double quiz_grades[5]; double average; } GRADE; STRING student_name; GRADE courses[10]; double gpa; } STUDENT_REC; int main(int argc, char *argv[]) { FILE *file_ptr = NULL; const int NUM_STUDENTS = 100; const int NUM_QUIZZES = 5; const int NUM_COURSES = 3; STUDENT_REC students[NUM_STUDENTS]; int i,j,k; double sum;

Without Functions /* open data file and input student data */ file_ptr = fopen(argv[1], "r"); if(file_ptr == NULL) printf("\n*** Error opening file: %s ***\n\n", argv[1]); else { for (i=0; i<=NUM_STUDENTS-1; i++) { fscanf(file_ptr,"%s %lf\n",students[i].student_name,&students[i].gpa); for (j=0; j<=NUM_COURSES-1; j++) { fscanf(file_ptr,"%s",students[i].courses[j].course_name); for (k=0; k<=NUM_QUIZZES-1; k++) { fscanf(file_ptr,"%lf",&students[i].courses[j].quiz_grades[k]); } smith 3.4 cse1001 94.2 97.0 96.4 93.9 100.0 mth2020 88.2 85.6 85.0 84.2 89.5 chm1400 75.6 72.3 77.0 76.1 70.2 jones 2.75 psy1201 74.2 77.0 76.4 73.9 70.0 aero2201 98.2 95.6 95.0 94.2 99.5 ece1040 85.6 82.3 87.0 86.1 80.2 :

Without Functions /* calculate averages */ for (i=0; i<=NUM_STUDENTS-1; i++) { for (j=0; j<=NUM_COURSES-1; j++) { sum = 0.0; for (k=0; k<=NUM_QUIZZES-1; k++) { sum+=students[i].courses[j].quiz_grades[k] } students[i].courses[j].average=sum/NUM_QUIZZES;

Without Functions /* output final results */ printf("\nOutput Final Student Data:\n"); printf("\n"); for (i=0; i<=NUM_STUDENTS-1; i++) { printf("student #%d: %s\ngpa: %lf\n\n", i+1,students[i].student_name,students[i].gpa); for (j=0; j<=NUM_COURSES-1; j++) { printf("%s\naverage: %lf: ", students[i].courses[j].course_name,students[i].courses[j].average); printf("\ngrades: "); for (k=0; k<=NUM_QUIZZES-1; k++) { printf("%lf ",students[i].courses[j].quiz_grades[k]); } printf("\n\n"); return 0;

Sample Output student #1: smith gpa: 3.400000 cse1001 average: 96.300000: grades: 94.200000 97.000000 96.400000 93.900000 100.000000 mth2020 average: 86.500000: grades: 88.200000 85.600000 85.000000 84.200000 89.500000 chm1400 average: 74.240000: grades: 75.600000 72.300000 77.000000 76.100000 70.200000 student #2: jones gpa: 2.750000 :

Exercises Type in, compile, and execute the program Clean up output formatting

With Functions A common design technique is to have a function to input a data structure. void input_student_data(FILE *fptr, STUDENT_REC student_list[]) { int i,j,k; for (i=0; i<=NUM_STUDENTS-1; i++) { fscanf(fptr,"%s %lf\n",student_list[i].student_name,&student_list[i].gpa); for (j=0; j<=NUM_COURSES-1; j++) { fscanf(fptr,"%s",student_list[i].courses[j].course_name); for (k=0; k<=NUM_QUIZZES-1; k++) { fscanf(fptr,"%lf",&student_list[i].courses[j].quiz_grades[k]); } return; /* Second parameter could also be STRUDENT_REC *student_list */

With Functions Similarly, it is common to have a function to output a data structure. void output_student_data(STUDENT_REC student_list[]) { int i,j,k; printf("\n"); for (i=0; i<=NUM_STUDENTS-1; i++) { printf("student #%d: %s\ngpa: %lf\n\n", i+1,student_list[i].student_name,student_list[i].gpa); for (j=0; j<=NUM_COURSES-1; j++) { printf("%s\naverage: %lf: ", student_list[i].courses[j].course_name,student_list[i].courses[j].average); printf("\ngrades: "); for (k=0; k<=NUM_QUIZZES-1; k++) { printf("%lf ",student_list[i].courses[j].quiz_grades[k]); } printf("\n\n"); return;

With Functions We will add one more to do the calculation…just for fun! void calculate_student_averages(STUDENT_REC student_list[]) { int i,j,k; double sum; for (i=0; i<=NUM_STUDENTS-1; i++) { for (j=0; j<=NUM_COURSES-1; j++) { sum = 0.0; for (k=0; k<=NUM_QUIZZES-1; k++) { sum+=student_list[i].courses[j].quiz_grades[k]; } student_list[i].courses[j].average=sum/NUM_QUIZZES; /* Parameter could also be STRUDENT_REC *student_list */

With Functions Now the main becomes: int main(int argc, char *argv[]) { FILE *file_ptr = NULL; STUDENT_REC students[100]; file_ptr = fopen(argv[1], "r"); if(file_ptr == NULL) printf("\n*** Error opening file: %s ***\n\n", argv[1]); else { input_student_data(file_ptr,students); fclose(file_ptr); calculate_student_averages(students); printf("\nOutput Final Student Data:\n"); output_student_data(students); } return 0;

With Functions Another version of the calculate function: void calculate_course_average(GRADE *course_data) { double sum = 0.0; int k; for (k=0; k<=4; k++) { sum+=course_data->quiz_grades[k]; } course_data->average = sum/NUM_QUIZZES; void calculate_student_averages(STUDENT_REC student_list[]) { int i,j,k; double sum; for (i=0; i<=NUM_STUDENTS-1; i++) { for (j=0; j<=NUM_COURSES-1; j++) { calculate_course_average(&student_list[i].courses[j]);