C language learned by suffering
C language learned by suffering
Exercise 15
fundamental knowledge
Q1-1
What do you call the number in memory attached to a variable?
Q 1-2
What do you call the method of handling the above number by assigning it to a variable?
program read-only
In the following program, even though the return value of the sum function is of type void
Briefly explain why it is possible to return the result of a calculation.
Briefly explain why it is possible to return the result of a calculation.
Q2-1
#include <stdio.h>
void sum(int, int, int *);
int main(void)
{
int value;
sum(50, 100, &value);
printf("%d\n", value);
return 0;
}
void sum(int min, int max, int *ans)
{
*ans = (min + max) * (max - min + 1) / 2;
return;
}
program writing
Q3-1
From multiple numbers entered in the range of 0-100.
Create a program to find and display the maximum and minimum values.
If -1 is entered, it is judged to be the end of the input.
However, the maximum and minimum values are obtained in one function other than the main function.
The number of elements of the array that stores the input numerical values shall be 10, and the number of elements of the array that stores the input numerical values shall be 10.
If more than that is entered, it is assumed that an error is unavoidable.
Hint: A -1 in the array indicates the end of the data.
Tip: To find the minimum value, repeat the comparison with the variable that stores the maximum value.
Create a program to find and display the maximum and minimum values.
If -1 is entered, it is judged to be the end of the input.
However, the maximum and minimum values are obtained in one function other than the main function.
The number of elements of the array that stores the input numerical values shall be 10, and the number of elements of the array that stores the input numerical values shall be 10.
If more than that is entered, it is assumed that an error is unavoidable.
Hint: A -1 in the array indicates the end of the data.
Tip: To find the minimum value, repeat the comparison with the variable that stores the maximum value.
descriptive expression
Q4-1
After all, briefly explain what a pointer is.
Basic Knowledge (sample answers)
Solution 1-1
address
Solution 1-2
pointer
Program reading (example solution)
Solution 2-1
The third argument of the sum function is declared as a variable of type pointer.
Because the contents of the variable can be directly rewritten by passing the address to it.
Because the contents of the variable can be directly rewritten by passing the address to it.
Program writing (example of solution)
Solution 3-1
#include <stdio.h>
void maxmin(int array[], int *max, int *min);
int main(void)
{
int i = 0, array[10], max, min;
do
{
printf("%dth number:", i + 1);
scanf("%d", &array[i]);
i++;
} while (array[i - 1] ! = -1);
maxmin(array, &max, &min);
printf("max %d : min %d\n", max, min);
return 0;
}
void maxmin(int array[], int *max, int *min)
{
int i = 0;
*max = 0;
*min = 100;
while (array[i] ! = -1)
{
if (array[i] > *max) *max = array[i];
if (array[i] < *min) *min = array[i];
i++;
}
}
*Maybe it was a little too challenging.
But if you can write a program as smooth as this one
This level is just barely acceptable for a professional programmer.
After further study, you can try again!
Short Answer Type (Sample Answers)
Solution 4-1
A shortcut variable for a variable.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.