EXERCISE Arrays, structs and file processing. Question You own a pet store. You want to keep an inventory of all the pets that you have. Pets available.

Slides:



Advertisements
Similar presentations
C: Advanced Topics-II Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Advertisements

Introduction to C Programming
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Getting Input in Python Assumes assignment statement, typecasts.
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
C Language.
 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
UNIT 15 File Processing.
Arrays and Strings.
What is shape function ? shape function is a function that will give the displacements inside an element if its displacement at all the node locations.
Standard I/O Lesson Outline
Standard I/O Lesson CS1313 Spring Standard I/O Lesson Outline 1.Standard I/O Lesson Outline 2.Output via printf 3.Placeholders 4.Placeholders for.
We Have Learned main() { … } Variable –Definition –Calculation –Display We can do some real programming! –Think about your solution design –Express design.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
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.
Structures in C.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Sort the given string, without using string handling functions.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
1 CSE1303 Part A Data Structures and Algorithms Semester 2, 2006 Lecture A1 – Welcome & Revision.
 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
CMPE13 Cyrus Bazeghi Hands on with Functions and IO.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
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.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
How to start Visual Studio 2008 or 2010 (command-line program)
Nested LOOPS.
CGS 3460 More On File IO. CGS 3460 What is a File n A file is a package of information with a name attached to it. n Files are used for various purposes:
Characters and Strings File Processing Exercise C Programming:Part 3.
1 CSE1301 Computer Programming: Revision. 2 Topics Type of questions What do you need to know? About the exam Exam technique Staff consultation Revision.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes C++ errors/debugging build/compile compiler does not recognize a statement build/compile.
EXERCISE Arrays, structs and file processing. Question An online store sells various types of children’s clothing. You are to create a program that stores.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Homework #2: Functions and Arrays By J. H. Wang Mar. 24, 2014.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
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.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
Computer Programming for Engineers
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
Simple “VICO” (“VIPO”) Programs (Variables, Input, Calculating or Processing, Output)
Files A collection of related data treated as a unit. Two types Text
In C programming, one of the frequently arising problem is to handle similar types of data. For example: If the user want to store marks of 100 students.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
Arrays. Arrays are objects that help us organize large amounts of information.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes Contrasting MATLAB with C MATLABC language Workspace - interactive computation No real.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
INC 161 , CPE 100 Computer Programming
CSCE 206 Lab Structured Programming in C
CSCE 206 Lab Structured Programming in C
Presentation transcript:

EXERCISE Arrays, structs and file processing

Question You own a pet store. You want to keep an inventory of all the pets that you have. Pets available are cats, birds and fish. Your store can only keep up to 40 pets at a time. Use a struct for each pet. Each pet will have an ID, type and price. Your program should read the input from keyboard, and then stores it into a file named “pets.dat”.

After data input, your program can – display all cats. – display the total price of all pets

Steps to solve 1)Define the struct struct sPet { int ID; char sType[5]; float price; };

2)Maximum pet is 40. So, use an Array of struct. struct sPet myPets [40] ;

You need to read from users ( = keyboard). Because you have an array of struct (myPets[x]), you can put the inputs there. printf (“Enter ID : “ ); scanf (“%d”, &myPets[x].ID); printf (“Enter type: “); scanf (“%s”, &myPets[x].sType);

Need to write into a file FILE *cPtr; cPtr = fopen (“pets.dat”, “w”); … … fprintf (cPtr, “\n %d %s %.2f”, myPets[x].ID, myPets[x].sType, myPets[x]. price) ; //this should be done in a loop

Because it is from user input, you must ask the how many pets he/she wants to enter the data int numPets; printf (“How many pets you want to enter ? Maximum is 40); scanf (“%d”, &numPets);

An array of pets, so must have ‘for’ loops for (x = 0; x < numPets; x++) {.. }

Need to calculate the total price, so add inside the loop for (x = 0; x < numPets; x++) {.. totalPrice + = myPets [ x ].price; } //then, outside the loop printf (“total Price: %.2f”, totalPrice);

To display the list of all cats, need another loop int a; //to hold value of compare string for ( x = 0; x < numItem; x++ ) { a = strcmp (myPets[x].sType, “cat”); if (a == 0) printf (“%d %s %.2f”, myPets[x].ID, myPets[x].sType, myPets[x].price) ; }

Skeleton of a full program #include struct sPet { int ID; char sType[5]; float price; };

int main() { struct sPet myPets [40] ; FILE *cPtr; cPtr = fopen (“pets.dat”, “w”); int numPets; float totalPrice = 0; printf (“How many pets you want to enter ? Maximum is 40); scanf (“%d”, &numPets);

for (x = 0; x < numPets; x++) { printf (“Enter ID : “ ); scanf (“%d”, &myPets[x].ID); printf (“Enter type: “); scanf (“%s”, &myPets[x].sType); printf (“Enter price: “); scanf (“%s”, &myPets[x].sType);

//continue loop block fprintf (cPtr, “\n %d %s %.2f”, myPets[x].ID, myPets[x].sType, myPets[x]. price) ; totalPrice + = myPets [ x ].price; } //end of loop 1 printf (“total Price: %.2f”, totalPrice);

int a; //to hold value of compare string for ( x = 0; x < numItem; x++ ) { a = strcpy (myPets[x].sType, “cat”); if (a == 0) printf (“%d %s %.2f”, myPets[x].ID, myPets[x].sType, myPets[x].price) ; } return 0; } //end of main