7. C program structure.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Computer Programming w/ Eng. Applications
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Computer Programming Lab 8.
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Lab 7 rAlternation rIteration Note: Read All Chapter 4(All Self-Check exercises and all remaining exercises).
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
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.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
Nested LOOPS.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Problem : At Kiddy School, the teachers used the A, B,C for grading the students’ works. If the student get an A, then the teacher will print “Excellent”
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Previously Repetition Structures While, Do-While, For.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Continue with conditional
Chapter 05 (Part III) Control Statements: Part II.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
1 Programming 2 Overview of Programming 1. Write the equations in C++ notation : a) R =   a + b  24  2 a  b) W = a 12 + b 2 – 2abcos(y) 2a.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes Contrasting MATLAB with C MATLABC language Workspace - interactive computation No real.
Repetition statements
ARRAYS.
Decision making If.. else statement.
User-Written Functions
ECE Application Programming
REPETITION STATEMENTS
CS 108 Computing Fundamental Notes for Thursday, October 5, 2017
FUNCTIONS EXAMPLES.
Control Statement Examples
INPUT & OUTPUT scanf & printf.
Functions.
SELECTION STATEMENTS (2)
1) C program development 2) Selection structure
Decision making If statement.
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
REPETITION STATEMENTS
Functions Extra Examples.
Continue with conditional
Variables in C Topics Naming Variables Declaring Variables
CSCE 206 Lab Structured Programming in C
EECE.2160 ECE Application Programming
Switch Case Structures
Presentation transcript:

7. C program structure

1. Structure of a c program Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization part The Input Processing (Assignment, if/switch, calculations, or any other C statements that will be covered later) The Output Return (0) command This is shown in the diagram of the next slide. Dr. Soha S. Zaghloul 2

1. Structure of a c program - summarized Preprocessor part (#include, #define) Main function int main (void) { // declaration part // initialization part // input part // processing // output Return (0); } // end of main function Dr. Soha S. Zaghloul 3

1.1 declaration part Preprocessor part (#include, #define) Main function int main (void) { // declaration part int number1, number2, number3; char gender; double salary; // initialization part // input part // processing // output Return (0); } // end of main function Dr. Soha S. Zaghloul 4

1.2 initialization part Preprocessor part (#include, #define) Main function int main (void) { // declaration part // initialization part total = 0; // previously declared double product = 1.0; // undeclared char name[20] = “xxxxxxxxxxxxxxxx”; //undeclared // input part // processing // output Return (0); } // end of main function Dr. Soha S. Zaghloul 5

1.3 input part Preprocessor part (#include, #define) Main function int main (void) { // declaration part // initialization part // input part printf (“Enter the values of three integers\n”); scanf (“%d%d%d”, &number1, &number2, &number3); // processing // output Return (0); } // end of main function Dr. Soha S. Zaghloul 6

1.4 processing part Preprocessor part (#include, #define) Main function int main (void) { // declaration part // initialization part // input part // processing total = number1 + number2 + number3; // output Return (0); } // end of main function Dr. Soha S. Zaghloul 7

1.4 processing part (2) Preprocessor part (#include, #define) Main function int main (void) { // declaration part // initialization part // input part // processing total = total + number1; product = product * number2; // output Return (0); } // end of main function Dr. Soha S. Zaghloul 8

1.4 processing part (3) Preprocessor part (#include, #define) Main function int main (void) { // declaration part // initialization part // input part // processing if (total == 0) total = total + number1; else product = product * number1; // output Return (0); } // end of main function Dr. Soha S. Zaghloul 9

1.5 output part Preprocessor part (#include, #define) Main function int main (void) { // declaration part // initialization part // input part // processing // output printf (“Final Results are: “); printf (“Total = %d, Product = %f”, total, product); Return (0); } // end of main function Dr. Soha S. Zaghloul 10

2. Example (1) Write a complete program that calculates the average of three integer numbers. Input? number1 number2 number3 Type? integer value? Through scanf Output? average Type? double value? To be calculated // declaration part int number1, number2, number3; double average; // input part printf (“Enter the values of the three integers: \n”); scanf (“%d%d%d”, &number1, &number2, &number3); Dr. Soha S. Zaghloul 11

2. Example (1) – cont’d Write a complete program that calculates the average of three integer numbers. #include <stdio.h> int main (void) { // declaration part int number1, number2, number3; double average; // input part printf (“Enter the values of the three integers: \n”); scanf (“%d%d%d”, &number1, &number2, &number3); // processing part average = (number1 + number2 + number3) /3.0; // output part printf (“The average equals %f “, average); return (0); } // end of main Dr. Soha S. Zaghloul 12

2. Example (2) Write a complete program to select an operation based on the value of inventory. Increment total_paper by paper_order if inventory is ‘B’ or ‘C’; increment total_ribbon by ribbon_order if inventory is ‘E’, ‘F’, or ‘D’; increment total_label by label_order if inventory is ‘A’ or ‘X’. Do nothing if inventory is ‘M’. Display an error message if the value of inventory is not one of these eight letters. input? inventory paper_order ribbon_order label_order type? char integer value? Through scanf // declaration part char inventory; int paper_order, ribbon_order, label_order; Dr. Soha S. Zaghloul 13

2. Example (2) – cont’d Write a complete program to select an operation based on the value of inventory. Increment total_paper by paper_order if inventory is ‘B’ or ‘C’; increment total_ribbon by ribbon_order if inventory is ‘E’, ‘F’, or ‘D’; increment total_label by label_order if inventory is ‘A’ or ‘X’. Do nothing if inventory is ‘M’. Display an error message if the value of inventory is not one of these eight letters. #include <stdio.h> int main (void) { // declaration part char inventory; int paper_order, ribbon_order, label_order; // input part printf (“Enter inventory \n”); scanf (“%c”, inventory); Dr. Soha S. Zaghloul 14

2. Example (2) – cont’d #include <stdio.h> int main (void) { Write a complete program to select an operation based on the value of inventory. Increment total_paper by paper_order if inventory is ‘B’ or ‘C’; increment total_ribbon by ribbon_order if inventory is ‘E’, ‘F’, or ‘D’; increment total_label by label_order if inventory is ‘A’ or ‘X’. Do nothing if inventory is ‘M’. Display an error message if the value of inventory is not one of these eight letters. // declaration part char inventory; int paper_order, ribbon_order, label_order; // input part printf (“Enter inventory \n”); scanf (“%c”, inventory); // iniatlization part int total_paper = 0; // processing part switch (inventory) { case ‘B’: case ‘C’: printf (“Enter amount of ordered paper \n”); scanf (“%d”, &paper_order); total_paper = total_paper + paper_order; printf (“Total paper = %d”, total_paper); break; Dr. Soha S. Zaghloul 15

2. Example (2) – cont’d #include <stdio.h> int main (void) { Write a complete program to select an operation based on the value of inventory. Increment total_paper by paper_order if inventory is ‘B’ or ‘C’; increment total_ribbon by ribbon_order if inventory is ‘E’, ‘F’, or ‘D’; increment total_label by label_order if inventory is ‘A’ or ‘X’. Do nothing if inventory is ‘M’. Display an error message if the value of inventory is not one of these eight letters. // declaration part char inventory; int paper_order, ribbon_order, label_order; // input part printf (“Enter inventory \n”); scanf (“%c”, inventory); // iniatlization part int total_paper = 0, total_ribbon = 0; // processing part // continuation of the switch statement case ‘E’: case ‘F’: case ‘D’: printf (“Enter amount of ordered ribbon \n”); scanf (“%d”, &ribbon_order); total_ribbon = total_ribbon + paper_order; printf (“Total ribbon = %d”, total_ribbon); break; Dr. Soha S. Zaghloul 16

2. Example (2) – cont’d #include <stdio.h> int main (void) { Write a complete program to select an operation based on the value of inventory. Increment total_paper by paper_order if inventory is ‘B’ or ‘C’; increment total_ribbon by ribbon_order if inventory is ‘E’, ‘F’, or ‘D’; increment total_label by label_order if inventory is ‘A’ or ‘X’. Do nothing if inventory is ‘M’. Display an error message if the value of inventory is not one of these eight letters. // declaration part char inventory; int paper_order, ribbon_order, label_order; // input part printf (“Enter inventory \n”); scanf (“%c”, inventory); // iniatlization part int total_paper = 0, total_ribbon = 0, total_label = 0; // processing part // continuation of the switch statement case ‘A’: case ‘X’: printf (“Enter amount of ordered label \n”); scanf (“%d”, &label_order); total_label = total_label + label_order; printf (“Total label = %d”, total_label); break; Dr. Soha S. Zaghloul 17

2. Example (2) – cont’d #include <stdio.h> int main (void) { Write a complete program to select an operation based on the value of inventory. Increment total_paper by paper_order if inventory is ‘B’ or ‘C’; increment total_ribbon by ribbon_order if inventory is ‘E’, ‘F’, or ‘D’; increment total_label by label_order if inventory is ‘A’ or ‘X’. Do nothing if inventory is ‘M’. Display an error message if the value of inventory is not one of these eight letters. // declaration part char inventory; int paper_order, ribbon_order, label_order; // input part printf (“Enter inventory \n”); scanf (“%c”, inventory); // iniatlization part int total_paper = 0, total_ribbon = 0, total_label = 0; // processing part // continuation of the switch statement case ‘M’: break; default: printf (“Invalid input \n”); //invalid inventory value break; } // end of switch statement return (0); } //end of main Dr. Soha S. Zaghloul 18

2. Example (2) – cont’d // processing part // Using if statement if (inventory == ‘B’) || (inventory == ‘C’) { printf (“Enter amount of ordered paper \n”); scanf (“%d”, &paper_order); total_paper = total_paper + paper_order; printf (“Total paper = %d”, total_paper); } // (inventory == ‘B’) || (inventory == ‘C’) else if (inventory == ‘E’) || (inventory == ‘F’) || (inventory == ‘D’) printf (“Enter amount of ordered ribbon \n”); scanf (“%d”, &ribbon_order); total_ribbon = total_ribbon + paper_order; printf (“Total ribbon = %d”, total_ribbon); } // (inventory == ‘E’) || (inventory == ‘F’) || (inventory == ‘D’) else if (inventory == ‘A’) || (inventory == ‘X) printf (“Enter amount of ordered label \n”); scanf (“%d”, &label_order); total_label = total_label + label_order; printf (“Total label = %d”, total_label); } // (inventory == ‘A’) || (inventory == ‘X) else if (inventory != ‘M’) // what if (inventory == ‘M)? printf (“Invalid Input \n”) Dr. Soha S. Zaghloul 19

2. Example (3) The table below shows the normal boiling points of several substances. Write a program that prompts the user for the observed boiling point of a substance in ºC. The program then identifies the substance if the observed boiling point is within 5% (more or less) of the expected boiling point. If the data input is more than 5% higher or lower than any of the boiling points in the table, the program should output the message “Substance unknown”. Substance Expected Boiling Point (ºC) Water 100 Mercury 357 Copper 1187 Silver 2193 Gold 2660 Dr. Soha S. Zaghloul 20

2. Example (3) The table below shows the normal boiling points of several substances. Write a program that prompts the user for the observed boiling point of a substance in ºC. The program then identifies the substance if the observed boiling point is within 5% (more or less) of the expected boiling point. If the data input is more than 5% higher or lower than any of the boiling points in the table, the program should output the message “Substance unknown”. Substance Expected Boiling Point (ºC) Water 100 Mercury 357 Copper 1187 Silver 2193 Gold 2660 Dr. Soha S. Zaghloul 21

2. Example (3) Input? observed Type? double value? Through scanf Output? substance Type? string value? To be calculated #include <stdio.h> int main (void) { // declaration part int observed; char substance[20]; // input part printf (“Enter the observed boiling point: \n”); scanf (“%d”, &observed); return (0); } // end of main Dr. Soha S. Zaghloul 22

2. Example (3) The table below shows the normal boiling points of several substances. Write a program that prompts the user for the observed boiling point of a substance in ºC. The program then identifies the substance if the observed boiling point is within 5% (more or less) of the expected boiling point. If the data input is more than 5% higher or lower than any of the boiling points in the table, the program should output the message “Substance unknown”. Substance Expected Boiling Point (ºC) Water 100 Mercury 357 Copper 1187 Silver 2193 Gold 2660 water_plus5 = (100 * 0.05) + 100; water_minus5 = (100 * 0.05) – 100; if (observed >= water_minus5) && (observed <= water_plus5) strcopy (substance, “water”); Dr. Soha S. Zaghloul 23

2. Example (3) The table below shows the normal boiling points of several substances. Write a program that prompts the user for the observed boiling point of a substance in ºC. The program then identifies the substance if the observed boiling point is within 5% (more or less) of the expected boiling point. If the data input is more than 5% higher or lower than any of the boiling points in the table, the program should output the message “Substance unknown”. Substance Expected Boiling Point (ºC) Water 100 Mercury 357 Copper 1187 Silver 2193 Gold 2660 in the same way, calculate: mercury_plus5, mercury_minus5, copper_plus5, copper_minus5, silver_plus5, silver_minus5, gold_plus5, gold_minus5 Dr. Soha S. Zaghloul 24

Don’t forget to declare your variables!!! 2. Example (3) The table below shows the normal boiling points of several substances. Write a program that prompts the user for the observed boiling point of a substance in ºC. The program then identifies the substance if the observed boiling point is within 5% (more or less) of the expected boiling point. If the data input is more than 5% higher or lower than any of the boiling points in the table, the program should output the message “Substance unknown”. Substance Expected Boiling Point (ºC) Water 100 Mercury 357 Copper 1187 Silver 2193 Gold 2660 Don’t forget to declare your variables!!! Dr. Soha S. Zaghloul 25

Complete the if statement…and conclude your program 2. Example (3) The table below shows the normal boiling points of several substances. Write a program that prompts the user for the observed boiling point of a substance in ºC. The program then identifies the substance if the observed boiling point is within 5% (more or less) of the expected boiling point. If the data input is more than 5% higher or lower than any of the boiling points in the table, the program should output the message “Substance unknown”. Substance Expected Boiling Point (ºC) Water 100 Mercury 357 Copper 1187 Silver 2193 Gold 2660 Complete the if statement…and conclude your program Dr. Soha S. Zaghloul 26

2. Example (4) Write a program that calculates and prints the bill for Riyadh’s power consumption. The rates vary depending on whether the user is residential, commercial, or industrial. A code of R corresponds to a Residential, C corresponds to a Commercial, and I to Industrial. Any other code should be treated as an error. The program should read the power consumption rate in KWH (Kilowatt per Hour); then it calculates the due amount according to the following: The rate is SAR 5 per KWH for Residential, SAR 10 per KWH for Commercial and SAR 20 per KWH for Industrial. Dr. Soha S. Zaghloul 27

2. Example (4) – input Write a program that calculates and prints the bill for Riyadh’s power consumption. The rates vary depending on whether the user is residential, commercial, or industrial. A code of R corresponds to a Residential, C corresponds to a Commercial, and I to Industrial. Any other code should be treated as an error. The program should read the power consumption rate in KWH (Kilowatt per Hour); then it calculates the due amount according to the following: The rate is SAR 5 per KWH for Residential, SAR 10 per KWH for Commercial and SAR 20 per KWH for Industrial. Dr. Soha S. Zaghloul 28

2. Example (4) – input Write a program that calculates and prints the bill for Riyadh’s power consumption. The rates vary depending on whether the user is residential, commercial, or industrial. A code of R corresponds to a Residential, C corresponds to a Commercial, and I to Industrial. Any other code should be treated as an error. The program should read the power consumption rate in KWH (Kilowatt per Hour); then it calculates the due amount according to the following: The rate is SAR 5 per KWH for Residential, SAR 10 per KWH for Commercial and SAR 20 per KWH for Industrial. Dr. Soha S. Zaghloul 29

2. Example (4) – output Write a program that calculates and prints the bill for Riyadh’s power consumption. The rates vary depending on whether the user is residential, commercial, or industrial. A code of R corresponds to a Residential, C corresponds to a Commercial, and I to Industrial. Any other code should be treated as an error. The program should read the power consumption rate in KWH (Kilowatt per Hour); then it calculates the due amount according to the following: The rate is SAR 5 per KWH for Residential, SAR 10 per KWH for Commercial and SAR 20 per KWH for Industrial. Dr. Soha S. Zaghloul 30

2. Example (4) – processing Write a program that calculates and prints the bill for Riyadh’s power consumption. The rates vary depending on whether the user is residential, commercial, or industrial. A code of R corresponds to a Residential, C corresponds to a Commercial, and I to Industrial. Any other code should be treated as an error. The program should read the power consumption rate in KWH (Kilowatt per Hour); then it calculates the due amount according to the following: The rate is SAR 5 per KWH for Residential, SAR 10 per KWH for Commercial and SAR 20 per KWH for Industrial. Dr. Soha S. Zaghloul 31