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 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 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 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 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 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 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 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 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 Write a complete program that calculates the average of three integer numbers. Input? Type? value? number1 number2 number3 integer integer integer Through scanf Through scanf Through scanf Output? Type? value? average double 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 Write a complete program that calculates the average of three #include <stdio.h> integer numbers. 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); Dr. Sohaend S. Zaghloul } // of main 12 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? type? value? inventory paper_order ribbon_order label_order char integer integer integer Through Through Through Through // declaration part char inventory; int paper_order, ribbon_order, label_order; Dr. Soha S. Zaghloul 13 scanf scanf scanf scanf 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 #include <stdio.h> ‘int A’ main or ‘X’. Do nothing if inventory is ‘M’. Display an error (void) {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); Dr. Soha S. Zaghloul 14 #include <stdio.h> Write complete program to select an operation based int maina (void) of { inventory. Increment total_paper by paper_order if on the value inventory is ‘B’declaration or ‘C’; increment total_ribbon by ribbon_order if inventory is // part char inventory; ‘E’, ‘F’, or ‘D’; increment total_label by label_order if inventory is int paper_order, ribbon_order, label_order; ‘A’ or ‘X’. Do nothing if inventory is ‘M’. Display an error // input part message if the value of\n”); inventory is not one of these eight letters. printf (“Enter inventory 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 #include <stdio.h> Write complete program to select an operation based int maina (void) {of inventory. Increment total_paper by paper_order if // declaration part ‘B’ or ‘C’; increment total_ribbon by ribbon_order if char inventory; ‘E’, ‘F’, or ‘D’;ribbon_order, increment total_label by label_order if int paper_order, label_order; on the value inventory is inventory is inventory is ‘// A’ input or ‘X’. part Do nothing if inventory is ‘M’. Display an error message if the value of\n”); inventory is not one of these eight letters. printf (“Enter inventory 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 #include <stdio.h> Write complete program to select an operation based int maina (void) of { inventory. Increment total_paper by paper_order if // part ‘B’declaration or ‘C’; increment total_ribbon by ribbon_order if char inventory; ‘E’, ‘F’, or ‘D’;ribbon_order, increment total_label by label_order if int paper_order, label_order; on the value inventory is inventory is inventory is ‘A’ or ‘X’. Do nothing if inventory is ‘M’. Display an error // input part message if the value of inventory is not one of these eight letters. 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 #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 // declaration part ‘B’ or ‘C’; increment total_ribbon by ribbon_order if inventory is char inventory; int label_order; ‘E’,paper_order, ‘F’, or ‘D’;ribbon_order, increment total_label by label_order if inventory is // part Do nothing if inventory is ‘M’. Display an error ‘A’ input or ‘X’. printf (“Enter inventory \n”); message if the value of inventory is not one of these eight letters. 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”); break; } // end of switch statement return (0); Dr. Soha S. Zaghloul } //end of main 18 //invalid inventory value // 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 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 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 Input? Type? value? observed double Through scanf Output? Type? value? substance string 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); Dr. Sohaend S. Zaghloul } // of main 22 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 water_plus5 = (100 * 0.05) + 100; boiling points =in the table, the program should output the water_minus5 (100 * 0.05) – 100; message “Substance unknown”. if (observed >= water_minus5) && (observed <= water_plus5) strcopy (substance, “water”); Substance Expected Boiling Point (ºC) Water 100 Mercury 357 Copper 1187 Silver 2193 Gold 2660 Dr. Soha S. Zaghloul 23 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 in the same way, calculate: boiling points in the table, the program should output the mercury_plus5, mercury_minus5, copper_plus5, copper_minus5, message “Substance unknown”. silver_plus5, silver_minus5, Substance Expected Boiling Point (ºC) gold_plus5, gold_minus5 Water 100 Mercury 357 Copper 1187 Silver 2193 Gold 2660 Dr. Soha S. Zaghloul 24 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 pointsDon’t in the table, the program should output the forget to declare your variables!!! message “Substance unknown”. Substance Expected Boiling Point (ºC) Water 100 Mercury 357 Copper 1187 Silver 2193 Gold 2660 Dr. Soha S. Zaghloul 25 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 Complete points inthethe table, the program should output the if statement…and conclude your program message “Substance unknown”. Substance Expected Boiling Point (ºC) Water 100 Mercury 357 Copper 1187 Silver 2193 Gold 2660 Dr. Soha S. Zaghloul 26 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 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 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 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 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