CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
 #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Arrays. INTRODUCTION TO ARRAYS Just as with loops and conditions, arrays are a common programming construct and an important concept Arrays can be found.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
CSE1301 Computer Programming: Revision. Topics Type of questions What do you need to know? About the exam Exam technique Sample questions.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Do you know the syntax for each of these, used to read and write to data files?  Pointers: think of it as the memory address of the file  fopen() 
Do you know the syntax for each of these, used to read and write to data files?  Pointers: think of it as the memory address of the file  fopen() 
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
C Programming – Part 6 File Input and Output
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 CSE1301 Computer Programming: Revision. 2 Topics Type of questions What do you need to know? About the exam Exam technique Staff consultation Revision.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
File IO and command line input CSE 2451 Rong Shi.
CECS 130 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
Do you know the syntax for each of these, used to read and write to data files?  Pointers: think of it as the memory address of the file  fopen() 
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
CECS 130 Final EXAM.  #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string.
Please enter how long your name is: 21 Please enter your name: Nawaf Hello Nawaf Please enter how long your name is: -7 Failed allocation memory.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
CECS 121 Final Test.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
CS 1704 Introduction to Data Structures and Software Engineering.
CECS 130 Mid-term Test Review Provided by REACH – Resources for Academic Achievement Presenter: [REACH Tutor]
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
CECS 121 Midterm Review Presented by REACH (If you have not signed the attendance sheet, please do so now!)
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Computer Science 210 Computer Organization
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
CECS 130 Mid-term Test Review
Yanal Alahmad Java Workshop Yanal Alahmad
Quiz 11/15/16 – C functions, arrays and strings
Chapter 18 I/O in C.
CECS 130 Mid-term Test Review
Week 4 – Repetition Structures / Loops
Module 2 Arrays and strings – example programs.
Programming in C Input / Output.
Computer Science 210 Computer Organization
CECS 130 Mid-term Test Review
CECS 130 Midterm Exam Review Spring 2019
CECS 130 Mid-term Test Review
Files Chapter 8.
Presentation transcript:

CECS 130 EXAM 1

 int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, L); printf (" %10d \n", 1977); printf ("%010d \n", 1977); printf ("floats: %4.2f \n", ); printf ("%s \n", "A string"); return 0; } }

printf (“%c %c \n", 'a', 65); aA printf ("%d %ld\n", 1977, L); printf (" %10d \n", 1977); 1977 printf ("%010d \n", 1977); printf ("floats: %4.2f \n", ); 3.14 printf ("%s \n", "A string"); A string

 What’s the syntax of  While(){}  Do{} While()  What’s the difference between While and Do{} While()?

 while ( condition ) { Code to execute while the condition is true }  do { } while ( condition );  Do{ } while() executes code at least once!

 Use when the number of iterations is already known  Syntax: for ( variable initialization ; condition ; variable increment/decrement ) { Code to execute while the condition is true }

#include int main() { int x; for ( x = 0; x < 10; x++ ){ printf( "%d\n", x ); } getchar(); }

 Write a program using a FOR Loop to display all of the multiples of 5 from 0 to 100.

#include int main() { int x; for ( x = 0; x < =20; x++ ) { printf( "%d\n", x*5 ); } getchar(); }

 Use to manipulate flow in loops  What does a break statement do when executed within a loop?  What does a Continue statement do when executed within a loop?

#include main() { int x; for ( x = 10; x >5; x-- ) { if (x==7) break; } printf( “\n %d \n ”, x ); } #include main() { int x; for ( x = 10; x >5; x-- ) { if (x==7) continue; printf( “\n %d \n ”, x ); }

 Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN)  Function Prototypes tell you the data type returned by the function, the data type of parameters, how many parameters, and the order of parameters  Function definitions implement the function prototype  Where are function prototypes located in the program?  Where do you find function definitions?

 Where are function prototypes located in the program?  Answer: before the main(){} function!  Function Definitions are self contained outside of the main(){} function

#include int mult ( int x, int y ); int main() { int x = 0; int y = 0; printf( "Please input two numbers to be multiplied: " ); scanf( "%d", &x ); printf( "%d ", x ); scanf( "%d", &y ); printf( "%d ", y); printf( "The product of your two numbers is %d\n", mult( x, y ) ); getchar(); } int mult (int x, int y) { return x * y; }

#include Void printReportHeader(); main() { printReportHeader; } void printReportHeader() { printf(“\n Column1\tColumn2\tColumn3\tColumn4 \n”) }

#include void printNumbers(); int iNumber = 0; main() { int x; for(x=0, x<10,x++) { printf(“\n Enter a number:”); scanf(“%d”, &iNumber); printNumbers(); } void printNumbers() { printf(“\n Your number is: %d \n”, iNumber); }

 Variable scope defines the lifetime of a variable  Local Scope: defined within functions and loses scope after function is finished. Can reuse in other functions (ex. p.123)  Global Scope: defined outside of functions and can be accessed by multiple functions

 Can you declare a one-dimensional array made up of 10 integers?  Answer: int iArray[10]  How to declare an Array  int iArray[10];  float fAverages[30];  double dResults[3];  short sSalaries [9];  char cName[19]; 18 characters and 1 null character

 Why do we initialize? Because memory spaces may not be cleared from previous values when arrays are created  Can initialize an array directly  Example int iArray[5]={0,1,2,3,4};  Can initialize an array with a loop such as FOR()

#include main() { int x; int iArray[5]; for( x=0; x < 5 ; x++) { iArray[x] = 0; }

 Can you add code to print out the values of the program below? #include main() { int x; int iArray[5]; for( x=0; x < 5 ; x++) { iArray[x] = 0; }

#include main() { int x; int iArray[5]; for( x=0; x < 5 ; x++) { iArray[x] = 0; } for(x=0 ; x<5; x++) { printf(“\n The value of iArray index %d is %d \n”, x, iArray[x]); }

 How do you search through an array?

#include main() { int x; int iValue; int iFound = -1; int iArray[5]; for( x=0; x < 5 ; x++) iArray[x] = (x+x); printf(“\n Enter value to search for:”); scanf(“%d”, &iValue); for(x=0 ; x<5; x++) { if( iArray[x] ==iValue) { iFound =x; break; } if(iFound >-1) printf(“\n I found your search value in element %d \n”, iFound); else printf(“\n Sorry, your search value was not found \n”); }

#include main() { char *str1 = “Michael”; char str2[] = “Vine”; printf(“\nThe length of string 1 is %d \n”, strlen(str1)); printf(“The length of string 2 is %d\n”, strlen(str2)); }

#include void convertL(char *); main() { char name1[] = “Michael”; convertL(name1); } void convertL(char *str) { int x; for ( x = 0; x <=strlen(str) ; x++) str[x] = tolower(str[x]); printf(“\nThe name converted to lower case is %s\n”, str); }

EntityDescription Bit Binary digit, 0 or 1Smallest value in a data file Byte Eight bitsStores a single character FieldGrouping of bytesi.e a word, social security number RecordGrouping of fieldsa single row of information, student name, age, ID, GPA FileGrouping of recordsseparate fields in a record using spaces, tabs, or commas

Kelly11/12/866Louisville Allen04/05/7749Atlanta Chelsea03/30/9012Charleston How many fields are there? How many records are there? How many bytes are there in the first record? How many bits are there in “Kelly”? How many fields are there? How many records are there? How many bytes are there in the first record? How many bits are there in “Kelly”?

Do you know the syntax for each of these, used to read and write to data files?  Pointers: think of it as the memory address of the file  fopen()  fclose()  fscanf()  fprintf()

 fopen() returns a FILE pointer back to the pRead variable #include main() { FILE *pRead; pRead = fopen(“file1.dat”, “r”); if(pRead == NULL) printf(“\nFile cannot be opened\n”); else printf(“\nFile opened for reading\n”); }

 Pretty basic.

 Reads a single field from a data file  “%s” will read a series of characters until a white space is found  can do fscanf(pRead, “%s%s”, name, hobby);

#include main() { FILE *pRead; char name[10]; pRead = fopen(“names.dat”, “r”); if( pRead == NULL ) printf( “\nFile cannot be opened\n”); else printf(“\nContents of names.dat\n”); fscanf( pRead, “%s”, name ); while( !feof(pRead) ) { printf( “%s\n”, name ); fscanf( pRead, “%s”, name ); }

Kelly11/12/866Louisville Allen04/05/7749Atlanta Chelsea03/30/9012Charleston Can you write a program that prints out the contents of this information.dat file?

#include main() { FILE *pRead; char name[10]; char birthdate[9]; float number; char hometown[20]; pRead = fopen(“information.dat”, “r”); if( pRead == NULL ) printf( “\nFile cannot be opened\n”); else fscanf( pRead, “%s%s%f%s”, name, birthdate, &number, hometown ); while( !feof(pRead) ) { printf( “%s \t %s \t %f \t %s\n”, name, birthdate, number, hometown ); fscanf( pRead, “%s%s%f%s”, name, birthdate, &number, hometown ); }

 The fprintf() function sends information (the arguments) according to the specified format to the file indicated by stream. fprintf() works just like printf() as far as the format goes. printf()

#include main() { FILE *pWrite; char fName[20]; char lName [20]; float gpa; pWrite = fopen(“students.dat”,”w”); if( pWrite == NULL ) printf(“\nFile not opened\n”); else { printf(“\nEnter first name, last name, and GPA separated” printf(“Enter data separated by spaces:”); scanf(“%s%s%f”, fName, lName, &gpa); fprintf(pWrite, “%s \t %s \t %.2f \n”, fName, lName, gpa); fclose(pWrite); }