500 likes | 619 Views
This guide highlights essential C programming techniques for processing integers and scores using arrays and functions. It covers input handling for ten integers, calculation of average scores, and the implementation of utility functions like calculating sums and setting array values to zero. Each function is designed for clarity and efficiency, allowing programmers to grasp fundamental C concepts related to input/output and array manipulation. Ideal for beginners looking to solidify their understanding of data handling in C.
E N D
1. ???? ????? ?????? ?' ???"?
2. ??????: ?????? ?? ????? ?????? ?? ????? ?????, ??????? ????? ?? ?? ?? ???? ??? ?? ??????.
???? ?????? ????? ?? ??? ?????? ????? ??? ??? ???. ????? ???? ????? "????". ???? ????? ???? ?????? ???. ????:
int A[10];
??? ??? ???????? ????? ?? ???? ??????, ??? ?-0. ????????? ?? ?? ????? ??? ??-??? ?? ????? ?????? ??????? ?? ?????? (???? ??????? ???????). ????:
A[0]=9;
3. ??????: ??????
?????? ???????? ?????? ????? ??????? ???? ?????? ????? ??? (???? ??? ?? ?? ??? ????????, ?????? ?? ?? ????????, ???"? ?? ?? ???? ???').
??? ??????, ?????? ?? ?????? ???? ????? ????? ?? ???' ??????? ?? ?????? ?????, ??? ????? ??? ??? ?? ?????? ????/??? ????????? ????? ???? ??????? ?????? ?? ?? ??? ????????.
4. ?????: ????? ?????? ???? ???? #include<stdio.h>
int main()
{
int i, input[10];
printf(Enter the 10 integers:\n);
for (i=0; i<10; i++)
scanf(%d, &input[i]);
for (i=9; i>=0; i--)
printf(%d, input[i]);
}
5. ?????:????? ??????? ???? ?????? #include<stdio.h>
int main()
{
int i;
double scores[10], average, sum=0;
printf(Enter the 10 scores:\n);
for (i=0; i<10; i++)
{
scanf(%lf, &scores[i] );
sum = sum + scores[i];
}
average=sum/10;
for (i=0; i<10; i++)
if (scores[i] > average)
printf(Score number %d is above average\n, i);
}
6. ??????: ?????? ?????? ????? ??????? ?????, ?? ?????? ???? ????? ????????? ????.
??? ??? ?????, ???? ???? (?????) ????? ?????? ??? ?????? ?????? ???? ?? ???????.
???? ??? ????? ?? ????? ???????? ??? ??? ?????? (??? ????) ????????? ?? ?????.
int days_in_month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
?? ???? ????? ???????? ?? ????, ?? ??? ???? ??? ???? ???? ???????. ?????? ???? ????? ??????:
int days_in_month[]={31,28,31,30,31,30,31,31,30,31,30,31};
?? ?????? ?????, ?? ??? ????? ?????? ??? ??? (?? ???? ??? ???? ???? ?? ?? ????? ???????).
int counters[10]={0};
7. ??? ???? ??? ????? ???-??? ???? ????? ????? ???? ?? ??????? ??? ??? ?????? ??? ?????.
??-???? ???? ????? ???? ????? ???-???:
???? ??-???? ???? ???-??? ?? ?? ?????, ????? ????? ??? ?"? ?????:
for(i=0; i<10; i++) A[i]=0; A=0
??-???? ?????? ???? ??? ????? ??? ?"? ????? =
?? ???? ?????:
for(i=0; i<10; i++) A[i]=B[i]; A=B
?? ?? ???? ?????? ??????, ?????, ???, ???' (????? ?????).
8. ????? ??????? ????? ?? ????? ??? ????? ???? ????? ??????, ??????? ????? ????? ?????? ?? ?????? ?????, ???????? "????".
????:
int a[10];
a[10]=0;
a[-1]=5;
9. ?????? ??????: ????? ?????
??????
10. ????? ???? ???????? ????? ???? ??? ???? ?????? ?? ???? ???-???, ??? ???? ?????? ?? ?? ????? ????????.
????-??? ????? ???????? ?? ?? ???? ?????, ??? ????????? ??? ?? ????? ?????? ???.
????:
sum=calc_sum(array, size);
????? ??????? ?????, ????????? ???? ???????? ??? ?? ????? ?????? ???, ????? ???????? ????? ????? ??????. ???????? ????? ?? ???? ????, ????? ?? ?? ?? ?? ???.
??? ??? ???? ????? ???? ???? ?-[ ] ?????? ????????, ?? ??? ?? ????? ????:
int calc_sum(int array[], int size);
11. ?????:??????? ?????? ???? ?? ????? #include <stdio.h>
int calc_sum(int arr[], int size)
{
int i, sum = 0;
for(i=0; i<size; i++) sum=sum+arr[i];
return sum;
}
int main()
{
int input[10], i;
for(i=0; i<10; i++) scanf("%d", &input[i]);
printf("The sum is %d\n", calc_sum(input, 10));
return 0;
}
12. ????? ???? ????????
????? ????????? ????? ?? ????? ?????? ???? ??? ?????? ????, ??????? ?? ????? ???????? ???? ?? ????? ?????? ????? ????? ?????? ?????.
????? ?? ?????? ???????? ??? ????? ?????, ????? ?????? ?? ????? ?????? ??????? (?????? ??? ???? ???? ?? ????? ?? ?????? ?? ??????).
13. ?????: ??????? ?????? ???? #include <stdio.h>
void zero_array(int arr[], int size)
{
int i;
for(i=0; i<size; i++) arr[i]=0;
}
int main()
{
int A[10], i;
zero_array(A,10);
for(i=0; i<10; i++) printf("%d", A[i]);
return 0;
}
14. ????? ??????-??: ???? ???? ??-???? ?????? ???? ?????? ????? (const) ???????? ??? ??? ?? ???? ????? ???????. ???? ?? ???? ?????? ???????? zero_array ????? ????? ????? ???:
int main()
{
const int A[10]={1,1,2,3,5,8,13,21,34,55};
int i;
zero_array(A,10);
return 0;
}
15. ????? ???? ???? ????????
???? ?????? ???? ???? ?? ???????? ?????? ??? ????? ????? ????? ????? (const).
???? ???????? ??? ?????? ?? ?????, ???? ?????? ?? ????? ?????? ?????, ??? ???? ?????? ???? ?? ?????? ??????:
void print_array(const int array[ ], size)
{
int i;
for(i=0; i<size; i++) printf(%d , array[i]);
}
16. ????? ???? ???????? - ?????
17. ??????: ?????? ??-??????? ?????? ????? ????? ???????, ?? ?????? ??????? ?????? ????.
???? ?????? ?????? ???? ???? ??-???:
int a[10][100];
??????? ??? ??????? 10 ?????? ????? 100 ?? ?????? ?????.
??? ???? ???? ??? ?? ?"? ??? ????????, ????:
a[5][8]=4;
18. ?????? ??-??????? - ?????? ???? ??? ????? ???????? ?? ????????? ???? ??-?????.
???????? ???? ??? ???? ?? ?????? ??-???????, ???????? ???? ?????? ??? ??? ???. ????:
int my_matrix[3][2]={ {1,0}, {0,1}, {1,1} };
?????? ???? ???? ??? ????? ?????? ??????? (?? ?? ???? ???? ?? ????? ????? ?? ???? ????).
19. ?????: ????? ???? ??-????? void zero_array(int a[100][100])
{
int i ,j;
for(i=0; i<100; i++)
for(j=0; j<100; j++)
a[ i ][ j ] = 0;
}
20. ?????: ????? ???? ??-????? void zero_array(int a[ ][100], int size1)
{
int i ,j;
for(i=0; i<size1; i++)
for(j=0; j<100; j++)
a[ i ][ j ] = 0;
}
21. ?????: ?????? ?????? ??-??????? int compare_arrays10(int a[10][10], int b[10][10])
{
int i, j, identical=1;
for(i=0; i<10; i++)
for(j=0; j<10; j++)
if (a[ i ][ j ] != b[ i ][ j ])
identical=0;
return identical;
}
22. ?????? ??-???????
??????
24. ??????? ?????? ??? ??? ????? ??? ?????? ????? (???? ???? ?? ????).
?????? ?????? ??????? ????? ????? ???? ????? ?? ?????. ????: char word[20];
??? ????? ????? ????? ??????? ?? ?????? ???-???, ??? ?? ????? ?? ????? (???? ????? ???? ???-??? ??? ??-??).
???-??, ?????? ???? ???? ??????? ?? ???? ???? (???? ????? ????? ???? ???? ???? ????? ?????).
???? ???? ?????? ?????????-????? ???? ????? ?????? ????? ????? ?? ???????, ???????? ?????? ?? ?????? ????.
25. ??????? ??????
??????, ?? ??? ???? ????? ????? ???? ?????? ?????? (????? ????? ?? ?????? ??????? ???????):
char message[ ]=Hello world!;
???? ?? ???????? ??? ??????? ??????, ??? ??????? ?????? ??? ?????? ???? ?? ????.
(????? ????? ????? ?????? ????? ???? ???? ?"? ?????? ??????).
26. ??????? - ?????? ????? ???????.
????? ??? ???????.
???/??? ?? ???????.
?????? ?? ???????.
27. ????? ??? ??????? ?? ???? ?? ????? ????? ??????, ?? ?? ?????? ????? ???? ???? ??????? ????? ??? \0 (??? ??? ????? 0 ?????(.
??? ?????? ?????, ?? ????? ????? ?????? ?? ?????? ??? ???? ???? ?? ????? (???? ??????? ??? ??? ?????? ???????? ???????).
?? ??????? ??? ??????? ???????? ??????? ???? ???????.
28. ??????? ????? ??? ???????
?? ??????? ????? ?? ??????? ?????? ????? ?????? ??????? ??? ?? ?? ?????.
???? ????? ??????? ???:
char word[]=HELLO;
????? ?????? ??????? ???:
char word[]={H,E,L,L,O,\0};
(????? ?????? ????? 5 ????? ???? ?? 6 ?????).
29. ??????? ?? ????
?? ???? ??? ???? ?? ????? ??? ?? ?????, ?????? ?????? ???? ?????? ?? ??????? (????? ?????), ?? ????? ?????? ?? ??? '0\' ???? ???????.
???? ?? ????? ?????? ??????? ?? ??????? ???? ?????:
char word[6]={H,E,L,L,O};
?? ????? ?????:
word[5]=\0;
30. ?????: ????? ???? ?????? ???????? ???? ????? ?????? ??????? ?? ????? ???, ?????? ?? ?? ??? ?? '0\' ???? ???????.
int my_strlen(char str[])
{
int i=0;
while (str[ i ] != \0)
i++;
return i;
}
32. ???/??? ?? ??????? ???? ????? ??????? ?? ???? ??????? ??? ??? ?? ????? scanf, printf ?? ????? (?? ????? getchar,putchar).
??????, ???? ??? ???? ???? ????? 200 ????? ??? ?????, ?????? ?? ????? ?????-???? (??? ????? ????? ????? ???????):
char answer[201];
int i=0;
do
{
scanf(%c, &answer[i]);
i++;
} while ((answer[i-1]!=\n) && (i<200))
answer[i]=\0;
33. ???/??? ?? ?????? ???? ???? ?? ????? ??????? ??????? ???-??? ?- scanf, printf ??????? ?????? %s. ????:
char answer[100];
scanf(%s, answer);
???? ???? ?? ?? ???? ?? ?????-???? (????? ?? ??? ????? ?????).
??? \0 ????? ???????? ?"? ?- scanf ???? ???? ??????? ??????.
???? ?? ??? ?????? ?? ????? & ?- scanf ?? ?????? ????.
34. ??? ?? ?????? ???? ??' ??????
?? ????? ???? ?????? ????? ???? ?????? ??????? ?????, ?? ??????? ????? "????", ?? ???? ???? ????? ???? ??????? ??????? ??. (????? ?? ????? ?????? ???? ??? ??? ?????).
?? ????? ?????? ?? ???? ?????? ?????? ?? ???? ????? ?? ?? ?- scanf. ????:
scanf(%40s, answer);
??? ???? ?????? ???? ?????? ??? ????? ????? ?????? ??-?? ??? ?????? ???? (?? ???? ?? ???? ???????? ?????).
35. ??? ?? ?????? ???? printf(%s, answer) ????? ?? ???? ??????? answer ?? ??? '0\'.
HELLO
?? ????? ??? ?? '0\' ???? ???????, ?? printf ????? ?? ?? ?? ??? ???? ??????? ???????, ?? ????? ????? ??? \0 (????? ????? ???? ??????? ??? ???? ???? ?? ?????). ????:
HELLO3894o9fhsdughw34oruvhjnxuioghyw89tpcvj
36. ??? ?????? ????/??? ?? ???????
?? ????? ??? ????? ????????: gets(str); ????? ?? ??????.
??? ?????? ???? ?? ???? ?????? ?? ???? ????, ?? ?? ?????? ????? ???? ???? ????? ??????? "????".
?? ?? ????? ??? ?????: puts(str); ?????? ??? printf(%s\n,str) (?????? ????? ???? ???? ???????).
38. ?????: ?????? ??????? ???? #include<stdio.h>
int main()
{
char word[30], spaced_word[60];
int i=0;
scanf(%29s, word);
while (word[i] != \0)
{
spaced_word[ i*2 ] = word[i];
spaced_word[ i*2+1 ] = ;
i++;
}
spaced_word[i*2-1] = \0 ;
printf(The word after spacing: %s\n, spaced_word);
}
39. ?????: ?????? ??????? ???? #include<stdio.h>
int main()
{
char word[30], spaced_word[60];
int i=0;
scanf(%29s, word);
while (word[i] != \0)
{
spaced_word[ i*2 ] = word[i];
spaced_word[ i*2+1 ] = ;
i++;
}
spaced_word[i*2-1] = \0 ;
printf(The word after spacing: %s\n, spaced_word);
}
41. ?????? ?? ??????? ?? ???? ???? ?????? ?? ?????? ???? ??? ?????? ?? ???? ????? ????:
string1==string2
string1=string2
?? ?????? ???? ??? ???? ?? ??????? ???? ?? ?????? ???????, ???? ?? ??????? ???? ?????? ?? ??????? ??? ?? ???? ???????.
?????? ?????? ?? ??????? ????? ???? ?????? ?????? string.h, ?????? ?????? ??? ?????? ???????, ????? ???????, ????? ?????? ??????? ???? ("??????"), ????.
42. ?????? string.h ???????? ?????? ????? ???? ??????? (??? ???? ??? '0\'):
int strlen(const char str[ ]);
(????? ?? ???? ?? ?-'0\')
?????? ???????:
int strcmp(const char str1[ ], const char str2[ ]);
??? ????? ??? ???????? ?????? ??????????? (??? ??? ??????, ??? ????-?????), ??????? 0 ?? ?? ????, ??' ????? ?? ??????? ????? ????, ??' ????? ?? ????? ????? ????.
43. - ??? ?? ?????string.h ?????? ????? ???? ?? ???? ?? ???????? ????? ??????, ??? ??????? ?? ?? ??? '0\' ???? ?? ??????.
????? ???? ?????? ???????:
int my_strcmp(char str1[], char str2[])
{
int i=0, same=0;
while ( (str1[i] == str2[i]) && (str1[i] != \0) ) i++;
if (str1[i] < str2[i]) same=-1;
else if (str1[i] > str2[i]) same=1;
return same;
}
44. ?????: ?? ???? ??????? ????? #include<stdio.h>
#include<string.h>
int main()
{
int bush_votes = 0 , kerry_votes=0;
char vote[6];
while(1)
{
scanf(%5s, vote);
if (strcmp(vote, Bush)==0) bush_votes++;
else if (strcmp(vote, Kerry)==0) kerry_votes++;
else if (strcmp(vote, Stop)==0) break;
else printf(Wrong vote!\n)
}
printf(Bush received %d votes and Kerry received %d votes\n,
bush_votes, kerry_votes);
}
45. - ??????? ??????string.h ????? ?????? ??????? ???? ????? ??-???:
strcpy(char target [], const char source[]);
?????? ??????? (????? ?? str2 ???? str1):
strcat(char str1[], const char str2[]);
????? ?????? ??????? ?? ?? ???????:
strchr(const char str[], char c);
????? ?????? ??????? ?? ?????? ??????? ????:
strstr(const char str[], const char find[]);
????? ???? ???? ?? ?????? ??????? ???? ??? ?????? ?????? ?????? ????.
46. ??? ?????? ???????? ?? ??????? ?????? stdlib.h ?? ???????? ?????? ?????? ????? ??? ?? ????.
int atoi(const char str[]);
double atof(const char str[]);
????: num=atoi(12345);
?? ?????? ???????? ????? ?????? ?????? ????? ?????? ???? ???? ????? ????? ?? ??????? ??????? ???????.
?? ?????? ?? ????? ???????? ?? ?????? (????????? ????? ??? ?????? ?????? ?????).
47. ?????? ???????? ?? ????? ??????ctype.h ??????? ???????? ?? ????? (??????? ??? ???? ?? ?????? ???? ??? ?? ?? ????). ????:
int isalpha(char c);
int islower(char c);
int isupper(char c);
int isdigit(char c);
char tolower(char c);
char toupper(char c);
(?? tolower ?? ????? ??? ????? ?? ??? ?? ???? ????, ??? ?? ?? toupper ?? ????? ??? ????).
- ?? ?????? ?????? ???????? ?? ???????.
48. ????? ??????-??: ??????? ??? ??????? ??? ?????? ?????? ???? ???? ??? ?????. ????: a
??? ??????? ??????? ?????? ???? ?????? ??????. ????: HELLO
?? ?????? a ?? ??????? ??? ?????? ??? ?? ?? ??? a ?????? ?? ??? \0. ????? ?? ???? ???? ????? a.
?? ??????? ?????? ?? ??????? ???? ?????? ?? ?????? ??????, ????:
strcmp(input,a)
???:
strcmp(input,a)
49. ??????? - ????? ?????? ??:
????? ???????.
????? ??? ???????.
???/??? ?? ???????.
?????? ?? ???????.