Any C Program consists of the following parts: Preprocessor part Main function. This consists of...
-
Upload
dina-stewart -
Category
Documents
-
view
225 -
download
0
Transcript of Any C Program consists of the following parts: Preprocessor part Main function. This consists of...
![Page 1: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/1.jpg)
7. C PROGRAM STRUCTURE
![Page 2: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/2.jpg)
1. STRUCTURE OF A C PROGRAM
Any C Program consists of the following parts:Preprocessor partMain 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
![Page 3: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/3.jpg)
1. STRUCTURE OF A C PROGRAM - SUMMARIZED
Preprocessor part (#include, #define)
Main function
int main (void){
Return (0);} // end of main function
// declaration part
// initialization part
// input part
// processing
// output
Dr. Soha S. Zaghloul 3
![Page 4: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/4.jpg)
Dr. Soha S. Zaghloul 4
1.1 DECLARATION PART
Preprocessor part (#include, #define)
Main function
int main (void){
Return (0);} // end of main function
// declaration partint number1, number2, number3;char gender;double salary;
// initialization part
// input part
// processing
// output
![Page 5: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/5.jpg)
Dr. Soha S. Zaghloul 5
1.2 INITIALIZATION PART
Preprocessor part (#include, #define)
Main function
int main (void){
Return (0);} // end of main function
// declaration part
// initialization parttotal = 0; // previously declareddouble product = 1.0; // undeclaredchar name[20] = “xxxxxxxxxxxxxxxx”; //undeclared// input part
// processing
// output
![Page 6: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/6.jpg)
Dr. Soha S. Zaghloul 6
1.3 INPUT PART
Preprocessor part (#include, #define)
Main function
int main (void){
Return (0);} // end of main function
// declaration part
// initialization part
// input partprintf (“Enter the values of three integers\n”);scanf (“%d%d%d”, &number1, &number2, &number3);// processing
// output
![Page 7: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/7.jpg)
Dr. Soha S. Zaghloul 7
1.4 PROCESSING PART
Preprocessor part (#include, #define)
Main function
int main (void){
Return (0);} // end of main function
// declaration part
// initialization part
// input part
// processingtotal = number1 + number2 + number3;
// output
![Page 8: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/8.jpg)
Dr. Soha S. Zaghloul 8
1.4 PROCESSING PART (2)
Preprocessor part (#include, #define)
Main function
int main (void){
Return (0);} // end of main function
// declaration part
// initialization part
// input part
// processingtotal = total + number1;product = product * number2;
// output
![Page 9: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/9.jpg)
Dr. Soha S. Zaghloul 9
1.4 PROCESSING PART (3)Preprocessor part (#include, #define)
Main function
int main (void){
Return (0);} // end of main function
// declaration part
// initialization part
// input part
// processingif (total == 0) total = total + number1;else product = product * number1;
// output
![Page 10: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/10.jpg)
Dr. Soha S. Zaghloul 10
1.5 OUTPUT PART
Preprocessor part (#include, #define)
Main function
int main (void){
Return (0);} // end of main function
// declaration part
// initialization part
// input part
// processing
// outputprintf (“Final Results are: “);printf (“Total = %d, Product = %f”, total, product);
![Page 11: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/11.jpg)
Dr. Soha S. Zaghloul 11
2. EXAMPLE (1)
Write a complete program that calculates the average of three integer numbers.
Input?
number1
number2
number3
Type?
integer
integer
integer
Output?
average
Type?
double
// declaration partint number1, number2, number3;double average;
value?
Through scanf
Through scanf
Through scanf
value?
To be calculated
// input partprintf (“Enter the values of the three integers: \n”);scanf (“%d%d%d”, &number1, &number2, &number3);
![Page 12: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/12.jpg)
Dr. Soha S. Zaghloul 12
2. EXAMPLE (1) – CONT’D
Write a complete program that calculates the average of three integer numbers.
// declaration partint number1, number2, number3;double average;
// input partprintf (“Enter the values of the three integers: \n”);scanf (“%d%d%d”, &number1, &number2, &number3);
// processing partaverage = (number1 + number2 + number3) /3.0;
// output partprintf (“The average equals %f “, average);
#include <stdio.h>
int main (void){
return (0);} // end of main
![Page 13: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/13.jpg)
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
integer
integer
value?
Through scanf
Through scanf
Through scanf
Through scanf
// declaration partchar inventory;int paper_order, ribbon_order, label_order;
Dr. Soha S. Zaghloul 13
![Page 14: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/14.jpg)
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.
// declaration partchar inventory;int paper_order, ribbon_order, label_order;
// input partprintf (“Enter inventory \n”);scanf (“%c”, inventory);
#include <stdio.h>int main (void){
Dr. Soha S. Zaghloul 14
![Page 15: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/15.jpg)
Dr. Soha S. Zaghloul 15
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.
// declaration partchar inventory;int paper_order, ribbon_order, label_order;
// input partprintf (“Enter inventory \n”);scanf (“%c”, inventory);
// processing partswitch (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;
// iniatlization partint total_paper = 0;
#include <stdio.h>int main (void){
![Page 16: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/16.jpg)
Dr. Soha S. Zaghloul 16
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.
// declaration partchar inventory;int paper_order, ribbon_order, label_order;
// input partprintf (“Enter inventory \n”);scanf (“%c”, inventory);
// 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;
// iniatlization partint total_paper = 0, total_ribbon = 0;
#include <stdio.h>int main (void){
![Page 17: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/17.jpg)
Dr. Soha S. Zaghloul 17
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.
// declaration partchar inventory;int paper_order, ribbon_order, label_order;
// input partprintf (“Enter inventory \n”);scanf (“%c”, inventory);
// 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;
// iniatlization partint total_paper = 0, total_ribbon = 0, total_label = 0;
#include <stdio.h>int main (void){
![Page 18: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/18.jpg)
Dr. Soha S. Zaghloul 18
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.
// declaration partchar inventory;int paper_order, ribbon_order, label_order;
// input partprintf (“Enter inventory \n”);scanf (“%c”, inventory);
// 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
// iniatlization partint total_paper = 0, total_ribbon = 0, total_label = 0;
#include <stdio.h>int main (void){
![Page 19: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/19.jpg)
Dr. Soha S. Zaghloul 19
2. EXAMPLE (2) – CONT’D// processing part// Using if statementif (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”)
![Page 20: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/20.jpg)
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 100Mercury 357Copper 1187Silver 2193Gold 2660
![Page 21: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/21.jpg)
Dr. Soha S. Zaghloul 21
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 100Mercury 357Copper 1187Silver 2193Gold 2660
![Page 22: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/22.jpg)
Dr. Soha S. Zaghloul 22
2. EXAMPLE (3)Input?
observed
Type?
double
Output?
substance
Type?
string
value?
Through scanf
value?
To be calculated
#include <stdio.h>
int main (void){
return (0);} // end of main
// declaration partint observed;char substance[20];
// input partprintf (“Enter the observed boiling point: \n”);scanf (“%d”, &observed);
![Page 23: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/23.jpg)
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 100Mercury 357Copper 1187Silver 2193Gold 2660
water_plus5 = (100 * 0.05) + 100;water_minus5 = (100 * 0.05) – 100;if (observed >= water_minus5) && (observed <= water_plus5) strcopy (substance, “water”);
![Page 24: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/24.jpg)
Dr. Soha S. Zaghloul 24
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 100Mercury 357Copper 1187Silver 2193Gold 2660
in the same way, calculate:mercury_plus5, mercury_minus5,copper_plus5, copper_minus5,silver_plus5, silver_minus5,gold_plus5, gold_minus5
![Page 25: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/25.jpg)
Dr. Soha S. Zaghloul 25
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 100Mercury 357Copper 1187Silver 2193Gold 2660
Don’t forget to declare your variables!!!
![Page 26: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/26.jpg)
Dr. Soha S. Zaghloul 26
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 100Mercury 357Copper 1187Silver 2193Gold 2660
Complete the if statement…and conclude your program
![Page 27: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/27.jpg)
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
![Page 28: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/28.jpg)
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
![Page 29: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/29.jpg)
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
![Page 30: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/30.jpg)
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
![Page 31: Any C Program consists of the following parts: Preprocessor part Main function. This consists of the following: The declaration part The Initialization.](https://reader030.fdocuments.in/reader030/viewer/2022033022/56649d825503460f94a676e3/html5/thumbnails/31.jpg)
2. EXAMPLE (4) – PROCESSINGWrite 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