【计算机程序设计week5主要内容】
1)分支控制的case switch
2)循环控制的do while ,while ,break,continue
3)数组,一维数组,二维数组,字符数组
教材参考:《王芳版》第3章、第4章
或《c primer plus》第6章、第7章、第10章
===============================================================
2)循环控制的do while ,while ,break,continue
3)数组,一维数组,二维数组,字符数组
教材参考:《王芳版》第3章、第4章
或《c primer plus》第6章、第7章、第10章
===============================================================
练习
1 Write a C program to find the factorial of a number, where the number is entered by user. (Hints: factorial of n = 1*2*3*...*n
#include <stdio.h>
int main(){
int number,factorial;
printf("Enter a number.\n");
scanf("%d",&number);
factorial=1;
while (number>0){ /* while loop continues util test condition number>0 is true */
factorial=factorial*number;
--number;
}
printf("Factorial=%d",factorial);
return 0;
}
2 Write a C program to add all the numbers entered by a user until user enters 0.
#include <stdio.h>
int main(){
int sum=0,num;
do /* Codes inside the body of do...while loops are at least executed once. */
{
printf("Enter a number\n");
scanf("%d",&num);
sum+=num;
}
while(num!=0);
printf("sum=%d",sum);
return 0;
}
3 Write a C program to find average of maximum of n positive numbers entered by user. But, if the input is negative, display the average(excluding the average of negative input) and end the program.
# include <stdio.h>
int main(){
float num,average,sum;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d",&n);
for(i=1;i<=n;++i){
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)
break; //for loop breaks if num<0.0
sum=sum+num;
}
average=sum/(i-1);
printf("Average=%.2f",average);
return 0;
}
4 Write a C program to find the product of 4 integers entered by a user. If user enters 0 skip it.
# include <stdio.h>
int main(){
int i,num,product;
for(i=1,product=1;i<=4;++i){
printf("Enter num%d:",i);
scanf("%d",&num);
if(num==0)
continue; / *In this program, when num equals to zero, it skips the statement product*=num and continue the loop. */
product*=num;
}
printf("product=%d",product);
return 0;
}
5 C program to find the sum marks of n students using arrays
#include <stdio.h>
int main(){
int marks[10],i,n,sum=0;
printf("Enter number of students: ");
scanf("%d",&n);
for(i=0;i<n;++i){
printf("Enter marks of student%d: ",i+1);
scanf("%d",&marks[i]);
sum+=marks[i];
}
printf("Sum= %d",sum);
return 0;
}
6 Write a C program to find sum of two matrix of order 2*2 using multidimensional arrays where, elements of matrix are entered by user.
#include <stdio.h>
int main(){
float a[2][2], b[2][2], c[2][2];
int i,j;
printf("Enter the elements of 1st matrix\n");
/* Reading two dimensional Array with the help of two for loop. If there was an array of 'n' dimension, 'n' numbers of loops are needed for inserting data to array.*/
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("Enter a%d%d: ",i+1,j+1);
scanf("%f",&a[i][j]);
}
printf("Enter the elements of 2nd matrix\n");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("Enter b%d%d: ",i+1,j+1);
scanf("%f",&b[i][j]);
}
for(i=0;i<2;++i)
for(j=0;j<2;++j){
/* Writing the elements of multidimensional array using loop. */
c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */
}
printf("\nSum Of Matrix:");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("%.1f\t",c[i][j]);
if(j==1) /* To display matrix sum in order. */
printf("\n");
}
return 0;
}
========================
上课的练习
/*
1 function
2 while
*/
#include "stdio.h"
void printMatrix(int bianchang){
int n ;
int i;
int j;
/* bianchang -> n*/
n=bianchang;
/*step 1 printf ***** */
i=0;
while(i<n){
printf("* ");
i=i+1;
}
printf("\n");
/*step 2 printf * * */
for(i=0;i<n-1;i++)
{
printf("* ");
for( j=0; j<n-2; j++)
printf(" ");
printf("*\n");
}
/*step 3 printf ***** */
i=0;
while(i<n){
printf("* ");
i=i+1;
}
}
void main()
{
int b;
printf("input bianchang please: ");
scanf("%d",&b);
while(b>0){
printMatrix(b);
printf("\ninput bianchang please: ");
scanf("%d",&b);
}
}
/*
1 array
2 for
3 break
*/
#include "stdio.h"
main()
{
int score[45];
int n;
int i;
int sum;
float avg;
for(i=0;i<45;++i){
/* input one student's score */
scanf("%d",&score[i]);
if(score[i]<0)
break;
}
n = i; /* the number of inputs*/
for(i=0;i<n;i++)
printf("score[%d] = %d \n ",i,score[i]);
sum=0;
for(i=0;i<n;i++)
sum += score[i];
avg = sum / (n*1.0);
printf("\n the avg is %.2f \n",avg);
}