DOC PREVIEW
Rose-Hulman CSSE 332 - Multi-dimensional Arrays

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CSSE 332Structures, Command Line Arguments, File Reading, and Functions2 Multi-dimensional arraysint points[3][4];points [1][3] = 12; /* NOT points[3,4] */printf(“%d”, points[1][3]);Multi-dimensional Arrays3Example 6:#include <stdio.h>struct birthday{int month;int day;int year;}; //Note the semi-colonint main() {struct birthday mybday; /* - „new‟ not needed ! *//* otherwise, it‟s just like Java ! */mybday.day=1; mybday.month=1; mybday.year=1977;printf(“I was born on %d/%d/%d\n”, mybday.day,mybday.month, mybday.year);}Structures Approximation of Java’s classes with only data encapsulation (no methods)4More on Structuresstruct person{char name[41];int age;float height;struct { /* embedded structure */int month;int day;int year;} birth;};struct person me;me.birth.year=1977;………/* array of info about everyone in class */struct person class[60]; class[0].name=“Gun”; class[0].birth.year=1971;……5typedef typedef struct person myPerson– Defines a new type name myPerson as a synonym for type struct personint main(){myPerson me;me.age = 6;…}6User-defined header files Structures and other data structures may be defined in a header file, for better organization of the code.  These are user-defined header files e.g. person.h To include it:#include “person.h’’ at the start of the program file.7Command line arguments Accept inputs through the command line. main(int argc, char* argv[])– argc – argument count– argv[] – value of each argument8Example 7#include <stdio.h>int main(int argc, char *argv[]){int count = 0;if(argc < 2){printf("Must enter at least one argument\n");printf("Example: ./a.out this is program 7\n");exit(1);}printf(" The number of arguments is %d\n", argc);printf("And they are :\n");while(count < argc){printf("argv[%d]: %s\n",count,argv[count] );count++;}printf("\n");return 0;}9File handling Open a file using “fopen” Returns a FILE pointer which is used to access the file. Modes:– Read(r) – Opens file for reading. Returns error (NULL) if file does not exist. – Write(w) – create a new file for writing (overwrite old one). File pointer at the beginning of file.– Append(a) – create a new file if file does not exist. Preserve the contents if file does exist and pointer at the end of the file. fprintf, fscanf, fclose10Example 8#include <stdio.h>int main(int argc, char *argv[]){/* Declare a file pointer */FILE *inFile=NULL; /* open file for writing*/inFile = fopen(“test.txt”, “w”);/* need to do explicit ERROR CHECKING */if(inFile == NULL){exit(1);}/* write some data into the file */fprintf(inFile, “Hello there\n”);/* don‟t forget to release file pointer */fclose(inFile); return 0;}11Reading till end of file int feof(FILE *) – The function is defined in stdio.h– Returns a non-zero value (TRUE) if end of file has been reached, and zero otherwise. Sample code:fscanf(inFile, "%d", &int1); // Try to readprintf("%d \n", int1); //Do something with the datawhile (feof(inFile) == 0 ){ //If there is data, enter loopfscanf(inFile, "%d", &int1); //Try reading againprintf("%d \n", int1); //Do something with the data} //go back to while to test if data was read12Functions - why and how ? If a problem is large Modularization –easier to: • code• debug Code reuse Passing arguments to functions– By value Returning values from functions– By value13Functions – basic exampleExample 9#include <stdio.h>/* function prototype at start of file */int sum(int a, int b); int main(int argc, char *argv[]){int total = sum(4,5); /* call to the function */printf(“The sum of 4 and 5 is %d\n”, total);}/* the function itself arguments passed by value*/int sum(int a, int b){return (a+b); /* return by value */}14Memory layout and addresses5 10 12.5 9. 8 r sint x = 5, y = 10;float f = 12.5, g = 9.8;char c = „r‟, d = „s‟;4300 4304 4308 4312 4316 4317x y f g c dHomework 3 Another exercise to get started on C. Available on class’s Angel page– follow link from the schedule


View Full Document

Rose-Hulman CSSE 332 - Multi-dimensional Arrays

Download Multi-dimensional Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Multi-dimensional Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Multi-dimensional Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?