Items in this page:
#include<stdio.h>
int main() { int k = 0, i, m; printf("Enter an integer as count: \n"); scanf_s("%d", &m); printf("Enter the %d integers: \n", m); for( i = 1; i <= m; i = i + 1) { scanf_s("%d", &k); printf("The data is: %d \n", k); } return 0; }
|
|
![]() | |
#include<stdio.h>
int main() { int n, m; printf("Enter an integer: \n"); scanf_s("%d", &m); printf("Enter another integer: \n"); scanf_s("%d", &n); for( ; m < n;) { printf("The data is: %d \n", m); m = n; scanf_s("%d", &n); } return 0; } // try these input pair: 3, 3 then 3, 4 and finally 4, 3
|
|
![]() | |
It is very interesting for us to study the flow and behavior of the program. During the debugging process it is a repetitive process of editing, re-editing and commenting out the code. For example by adding suitable codes in the previous program as shown, we can see the program flow and behavior clearer as shown by a flowchart and a more details by tracechart.
#include <stdio.h>
int main() { int n, m; printf("Enter an integer: \n"); scanf_s("%d", &m); printf("Enter another integer: \n"); scanf_s("%d", &n); // see the current values printf("The data m = %d and n = %d\n", m, n); for( ; m < n;) { printf("The data is: %d \n", m); // assign n's value to m m = n; // see the current values printf("The data m = %d and n = %d\n", m, n); // waiting or reading next input scanf_s("%d", &n); // see the current values printf("The data m = %d and n = %d\n", m, n); } return 0; } | |
Now change the conditional operator as shown below. Can you determine what will be printed?
#include<stdio.h>
int main() { int n, m; printf("Enter an integer: \n"); scanf_s("%d", &m); printf("Enter another integer: \n"); scanf_s("%d", &n); for( ; m != n; ) { printf("The data is: %d \n", m); m = n; scanf_s("%d", &n); } return 0; }
|
|
#include<stdio.h>
int main() { int k = 0, i; printf("Enter integers: \n"); scanf_s("%d", &k); for(i = 0; k != 11; i = i + 1) { printf("The data is: %d \n", k); scanf_s("%d", &k); } printf("\n%d\n", i); return 0; }
|
|
#include<stdio.h>
int main() { int k = 0, i, sum = 0; printf("Enter a sample input: \n"); scanf_s("%d", &k); for(i = 0; k != 11; i = i + 1) { sum = sum + k; scanf_s("%d", &k); } printf("sum = %d, i = %d, ((sum*1.5)/i) = %.2f\n", sum, i, ((sum*1.5)/i)); return 0; }
|
|
![]() | |
Show the output for the following programs and from the output, study the program behaviors.
#include<stdio.h>
int main() { int k = 0, i, sum1 = 0, sum2 = 0; printf("Enter the sample input: \n"); for(i=1; i<=2; i=i+1) { scanf_s("%d", &k, 1); sum1 = sum1 + k; scanf_s("%d", &k, 1); sum2 = sum2 + k; } printf("sum1 = %d, sum2 = %d\n", sum1, sum2); return 0; } // sample input: 2 40 30 90 10 40 on one line or separate line | |
#include<stdio.h>
int main() { int k = 0, sum = 0; printf("Enter the sample input: \n"); scanf_s("%d", &k, 1); for(; k != 10;) { sum = sum - k; scanf_s("%d", &k, 1); printf("sum = %d ", sum); } printf("\nfinal sum = %d\n", sum); return 0; } // sample input: 2 40 30 90 10 40 on one line or separate line | |
Write C code snippet solutions for the following problem statements. Before writing and testing the codes you might want to create a pseudocode and a flowchart and/or tracechart.
Ask the user how many numbers to be added. Read in that many numbers and print their sum at the end. | #include <stdio.h>
int main() { int i, k, j = 0; double sum = 0.0; // prompt the number of count printf("Enter number count to be summed up: \n"); // read and store the count scanf_s("%d", &k, 1); // prompt for the integers printf("Enter those integers: \n"); for(i = 1; i <=k; i++) { // start reading & storing the first input and so on scanf_s("%d", &j, 1); // sum the input sum = sum + j; // check, i <= k?, if TRUE, repeat, else // stop or exit the loop } // print the final sum printf("\nFinal sum = %.2f\n", sum); return 0; }
|
|
|
Read in grades for two quizzes (floats) on each line (for each student) until a -1 is entered for the first quiz. Print the average of each quiz at the end. | #include <stdio.h>
int main() { int i; float mark = 0.0, sum = 0.0; printf("Enter the 1st & 2nd marks, 1st mark -1 to stop: \n"); scanf_s("%f", &mark); for(i = 0; mark != -1.0; i = i + 1) { sum = sum + mark; scanf_s("%f", &mark); } printf("Total mark = %.2f, number of mark = %d,\n Average: % .2f\n", sum, i, (sum/i)); return 0; }
|
|
|
Read in grades with a value between 0 and 100 until a negative grade is read in. Stop the loop once a negative grade is read in. At the end print the average of all the other grades except for the negative one. For your own observation: Change the terminal condition in the for statement: (mark > 0) & (mark <100) to the following, rebuild and re-run your program and see the effect, error or warning.
| #include <stdio.h>
int main() { int i; float mark = 0.0, sum = 0.0; printf("Enter marks, -ve to stop: \n"); scanf_s("%f", &mark); for(i = 0; (mark > 0) & (mark <100); i = i + 1) { sum = sum + mark; scanf_s("%f", &mark); } printf("Total mark = %.2f, number of mark = %d,\n Average: %.2f\n", sum, i, (sum/i)); return 0; }
|
|
|
Keep reading in characters until two consecutive characters are equal. Then print the total number of characters except for the last two.
| #include <stdio.h> int main() { char n, m; int i; // read char & store in m printf("Enter a character, same char to terminate: "); scanf_s(" %c", &m); // then read char & store in n printf("Enter another character, same char to terminate: "); scanf_s(" %c", &n); // if m!=n, that is different char, execute for loop.. // else just go to the next statement // after the for loop body // i used to calculate the total character minus the // last similar character for(i = 0; m != n; i=i+1) { printf("More character, same char to terminate: "); // assign n's value to m, so m will hold n's value m = n; // then read next char & store in n, so // n will hold new value, different with m // Then both will hold fresh values... // though the first n and m are outside the for loop... scanf_s(" %c", &n); } printf("Total character: %d\n", i); return 0; }
|
More Practice
Run the following programs and answer the questions.
#include<stdio.h>
int main() { int i, k, total; total = 0; printf("Enter 3 integers: "); for(i=1; i <=3; i=i+1) { // 1st iteration, read and store the first input // next iteration, read and store next input scanf_s("%d", &k); // sum up for every iteration total = total + k; // increment i & check the terminal condition } printf("Total = %d\n", total); return 0; }
1. At the end of the first iteration? 2. At the end of the second iteration? 3. At the end of the third iteration? 4. After the loop when it is printed?
|
|
| |
#include<stdio.h>
int main() { int i, k, total; total = 0; printf("Enter an integer:\n"); scanf_s("%d", &k, 1); printf("More integers:\n"); for(i=1; i <=3; i=i+1) { total = total + k; scanf_s("%d", &k, 1); } printf("Total = %d\n", total); return 0; }
|
|
#include<stdio.h>
int main() { int i, k, total; total = 0; printf("Enter an integer:\n"); scanf_s("%d", &k, 1); printf("More integers:\n"); for(i=1; i <=3; i=i+1) { scanf_s("%d", &k, 1); total = total + k; } printf("Total = %d\n", total); return 0; } // try the sample input: 40, 20, 50 and 10
|
|
#include<stdio.h>
int main() { int i, k, total; total = 0; printf("Enter an integer:\n"); scanf_s("%d", &k, 1); printf("More integers:\n"); for(i=1; i <=3; i=i+1) scanf_s("%d", &k, 1); // statement 1 total = total + k; // statement 2 printf("Total = %d\n", total); return 0; } // sample input: 40, 20, 50 and 10
|
|
Using the following for loop:
for(i=1; i <= count; i = i + 1)
Write a program that will first read the variable count as a data item, read in that many more data items and print theirtotal. For example, if the data were 3,40, 20,50, and 10;count would become 3 and the sum of the next 3 data items would be calculated as 110.
|
|
#include<stdio.h>
int main() { int i, k; printf("Enter an integer:\n"); scanf_s("%d", &k, 1); for(i=1; k !=0; i=i+1) scanf_s("%d", &k, 1); printf("i = %d\n", i); return 0; } // sample input: 4, 2, 9, 11, 4 and 0
|
|
![]() |