taeyounkim LOG
pointer practice 6 (program to print biggest and second biggest num in an array + program that returns the biggest element in an array+money paying program) 본문
pointer practice 6 (program to print biggest and second biggest num in an array + program that returns the biggest element in an array+money paying program)
taeyounkim 2021. 7. 9. 07:57#include <stdio.h>
#define N 10
void find_two_largest(int a[], int n, int *largest, int *second_largest);
int main()
{
int b[N], i, j;
int num;
printf("Enter 10 Numbers: ");
for(num=0; num < N; num++){
scanf("%d", &b[num]);
}
find_two_largest(b, N, &i, &j);
printf("%d, %d", i, j);
return 0;
}
void find_two_largest(int a[], int n, int *largest, int *second_largest)
{
int i;
*largest = *second_largest = a[0];
for(i=1; i<n; i++){
if(a[i]>*largest)
*largest = a[i];
else
continue;
}
for(i=1; i<n; i++){
if(a[i]>*second_largest && a[i]<*largest)
*second_largest = a[i];
else
continue;
}
}
#include <stdio.h>
#define N 10
int *find_largest(int a[], int n);
int main()
{
int b[N];
int num;
int *p;
printf("Enter 10 numbers: ");
for(num=0; num < N; num++){
scanf("%d", &b[num]);
}
p = find_largest(b, N);
printf("%d", *p);
return 0;
}
int *find_largest(int a[], int n)
{
int i;
int *k = &a[0];
/*The address of a[0] is copied into k.
Make sure NOT TO FORGET about the '&' in front of the a[0],
OTHERWISE, you will get an error message.*/
for(i=1; i<n; i++){
if(a[i] > *k)
*k = a[i];
else
continue;
}
return k;
}
#include <stdio.h>
void pay_amount(int dollars, int *twenties, int *tens, int *fives, int *ones);
int main()
{
int money, a, b, c, d;
printf("Enter amount of money: ");
scanf("%d", &money);
pay_amount(money, &a, &b, &c, &d);
printf("20s: %d, 10s: %d, 5s: %d, 1s: %d", a, b, c, d);
return 0;
}
void pay_amount(int dollars, int *twenties, int *tens, int *fives, int *ones)
{
*twenties = dollars / 20;
int k = dollars - *twenties * 20;
*tens = k / 10;
int i = k - *tens * 10;
*fives = i / 5;
int j = i - *fives * 5;
*ones = j;
}
'Career&Study > Coding Practice' 카테고리의 다른 글
pointer practice 5 (function that prints how much time passed since midnight) (0) | 2021.07.08 |
---|---|
pointer practice 4 (function to print bigger integer) (0) | 2021.07.08 |
pointer practice 3 (function to swap numbers) (0) | 2021.07.08 |
pointer practice 2 (program to find the largest and smallest elements in an array) (0) | 2021.07.07 |
pointer practice (decomposing fraction program) (0) | 2021.07.07 |