6.1.
Write program using pointer
The function should receive three integer pointer:
1. Increase 1st argument by 1
2. Multiply 2nd argument by 3rd argument
3. Assign the result to 2nd argument
4. Swap 2nd and 3rd argument
#include<stdio.h>
cal(int *a, int *b, int *c) {
(*a)++;
(*b) *= (*c);
int temp = *b;
*b = *c;
*c = temp;
}
int main()
{
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
printf("Before: a = %d, b = %d, c = %d\n", a, b, c);
cal(&a, &b, &c);
printf("After: a = %d, b = %d, c = %d\n", a, b, c);
return 0;
}
6.2.
Write a function that receives two strings and compares the input strings.
If two strings are same, return 1; otherwise, return 0.
char string1[80]; char string2 [80];
Using a call-by-reference
#include<stdio.h>
int check_string(char str1[80], char str2[80]) {
//const는 값을 바꿀 수 없는 상태가 된다 > 읽기만 가능
while(*str1!='\0'||*str2!='\0'){
if(*str1++!=*str2++)
return 0;
}
return 1;
}
int main()
{
char str1[80], str2[80];
printf("Enter two strings : ");
scanf("%s %s",str1, str2);
if (check_string(str1, str2))
printf("%s and %s are equal.", str1, str2);
else
printf("%s and %s are not equal.", str1, str2);
//삼항연산자 strcmp
return 0;
}
6.3.
Write a program that inputs two strings, and concatenates the two strings into the first one
(e.g., s1: Hello, s2:World -> s1: HelloWorld, s2: World)
char string1[80]; char string2 [80];
Using a call-by-reference
#include<stdio.h>
void concatenation(char *str1, char *str2) {
while (*str1++ != '\0');
*str1--;
while (*str1++=*str2++);
}
int main()
{
char str1[80], str2[80];
printf("Enter two strings : ");
scanf("%s %s",&str1,&str2);
concatenation(str1,str2);
printf("%s",str1);
return 0;
}
6.4.
Write a program that takes operation mode and two input numbers. The program outputs the result with a full equation.
Using a function pointer
#include<stdio.h>
int add(int *a, int *b);
int sub(int *a, int *b);
int mul(int *a, int *b);
int main()
{
int (*func[3])(int, int) = { add, sub, mul };
char operator[3]={'+','-','*'};
int select, num1, num2;
//size_y select;
printf("0.Addition\n1.Subtraction\n2.Multiplication\n3.End\n");
printf("select the operation : ");
scanf("%d",&select);
if(select!=3){
printf("Enter the two numbers : ");
scanf("%d %d",&num1,&num2);
printf("\n%d %c %d = %d\n",num1,operator[select],num2,func[select](&num1,&num2));
}
return 0;
}
int add(int *a, int *b){
return *a+*b;
}
int sub(int *a, int *b){
return *a-*b;
}
int mul(int *a, int *b){
return *a**b;
}
6.5.
Write a program that print out an array with inverse order
Using rand() function to initialize an array
When you print it out, use pointer operator to access elements of the array
#include<stdio.h>
#include<time.h>
int main()
{
srand(time(NULL));
int arr[10];
int *inverse=arr;
int i;
for(i=0;i<10;i++)
arr[i]=rand()%99+1;
for(i=0;i<10;i++)
printf("%2d ",*inverse++);
printf("\n");
for(i=0;i<10;i++)
printf("%2d ",*--inverse);
return 0;
}
'학부공부 > C_Basic' 카테고리의 다른 글
8. Bit operator[C Basic] (0) | 2021.11.07 |
---|---|
7. String[C Basic] (0) | 2021.11.07 |
5. Array and Functions[C Basic] (0) | 2021.11.07 |
4. Function[C Basic] (0) | 2021.11.07 |
3. Loop[C Basic] (0) | 2021.11.07 |
댓글