Let look at this code snipset
#include <stdio.h>
int main(void)
{
int a , b , c;
int max;
printf("Getting a max value of 3");
printf("a: "); scanf("%d", &a);
printf("b: "); scanf("%d", &b);
printf("c: "); scanf("%d", &c);
max = a;
if(b > max) max = b;
if(c > max) max = c;
prinft("Max value is %d", max);
return 0;
}
scanf need to use & (Ampersand) .. Lets not go super deep at this point but..!
Lets think scanf need to get variable address by & and dump value to the address.
Just remember at this point '&' gets the address value of the variable.
This code shows super basic algorithm.
1. put max value in a
2. If b is bigger than max put b in max
3. If c is bigger than max put c in max
This is called concatenation structure / algorithm that is runs in order (top to bottom)
So we can think Algorithm as a just bunch of if and looping statement.
Next example is a basic looping code
int i, n;
int sum;
printf("Print 1 - n");
scanf("%d", &n);
sum = 0;
for (int i = 0; i < n; i++)
{
sum += i;
}
prinf("Sum of 1 to n i s %d");
The loop statement loops till when i reach to n.
So we can think Algorithm as mixing the if and loop statement to get the answer ( and some recursion .. which I hate).
'Computer Science > Algorithm and Data structure with C' 카테고리의 다른 글
| Leetcode #78 Subse (0) | 2023.05.29 |
|---|---|
| Chapter 2 Array (0) | 2023.02.16 |