Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
1. nested loop, printing characters in the following format
#
##
###
####
#####
嵌套循环解释:
for(){
for(){
}
}
Solution: (* Note prov-for.c)
for(i=0;i<5;i++){
for(j=0;j<=i;j++){
printf("#");
}
printf("\n");
}
2. Use switch calculating a score, the score input, the output level
90-100 | A |
---|---|
70-89 | B |
60-69 | C |
<60 | D |
Solution: (* prov-switch.c)
int i;
printf("input sorce:\n");
scanf("%d",&i);
i=i/10;
switch(i){
case 10:
case 9:
printf("A\n");
break;
case 8:
case 7:
printf("B\n");
break;
case 6:
printf("C\n");
break;
default:
printf("D\n");
break;
}
3. Design a function chline (ch, i, j), i-th row specified character printing, j column.
Example: chline ( 'a', 3 , 2) Output:
AA
AA
AA
Solution: (* Note prov-chline.c)
viod chline(char ch,int i,int j){
int a;
int b;
for(a=0;a<i;a++){
for(b=0;b<j;b++){
printf("%s",ch);
}
printf("\n");
}
}
4. Write a function mymax (int array []), returns the maximum value stored in the array of type int.
Solution: (* prov-max.c)
int mymax(int aray[],int n){
int i;
int max;
max = array[0];
for(i=0;i<n;i++){
if(max<array[i]){
max=array[i];
}
}
return max;
}
5. Design a GetMemory function, the application 100 bytes of memory, and the memory is transmitted back
Solution: (** Note # prov-getmemory.c)
method one:
char *getmemory(){
char *p=malloc(100);
return p;
}
Method two :( achieved by passing the two pointers)
void getmemory(char **p){
*p=malloc(100);
}
summary: Compare ignorant of the fifth question, the understanding of a pointer and secondary pointer is not accurate enough place, resulting in using up more confusion; All in all, also to be strengthened.