Tasks studies - laboratory
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
int main(int argc, char const *argv[])
{
int array[3]; // Declaration of an array of size 3
array[0] = 1; // Setting the next values in the array
array[1] = 2;
array[2] = 3;
array[3] = 4; // Behavior "Undefined" because we are going beyond the range of the array. The value may change during runtime because the program did not reserve this space in memory.
printf("Array size in bytes: %i\n",sizeof(array));
printf("Because the size of int - %i bytes times the size of the array - 3\n", sizeof(int));
printf("array[0] = %i\n", array[0]);
printf("array[1] = %i\n", array[1]);
printf("array[2] = %i\n", array[2]);
printf("array[3] = %i\n", array[3]); // outside the scope of the declared array
printf("array[4] = %i\n", array[4]); // outside the scope of the declared array
return 0;
}
The following literal allows you to define an array of a given size and immediately assign values to subsequent fields of the array.
int array[3] = {1,2,3};
Or without specifying the size. The compiler will read it from the literal declaring the values.
int array[] = {1,2,3};
Defining a multidimensional array (array in an array):
[[1,2,3],
[4,5,6]]
int main(int argc, char const *argv[])
{
int array[2][3];
// in the array at index 0 there is an array storing a 3-element int array
array[0][0] = 1;
array[0][1] = 2;
array[0][2] = 3;
// in the array at index 1 there is an array storing a 3-element int array
array[1][0] = 4;
array[1][1] = 5;
array[1][2] = 6;
return 0;
}
Defining and assigning values to a multidimensional array.
char chararray[3][2] = { {'a','b'},
{ 'c','d' },
{ 'e','f' } };
printf("%c",chararray[1][1]);
break
- interrupts the loop.
#include <stdio.h>
int main(int argc, char const *argv[])
{
for(int i = 0; i<10;i++){
if (i == 5){
break;
}
printf("i = %i\n",i);
}
return 0;
}
In the c99 standard, you can’t declare a value in a loop like in the above example.
#include <stdio.h>
int main(int argc, char const *argv[])
{
int i;
for(i = 0; i<10;i++){
if (i == 5){
break;
}
printf("i = %i\n",i);
}
return 0;
}
continue
- moves to the next iteration.
#include <stdio.h>
int main(int argc, char const *argv[])
{
for(int i = 0; i<10;i++){
if (i == 5){
continue;
}
printf("i = %i\n",i);
}
return 0;
}
task1.exe
Create a program that stores the data of the following table: | 3.1 | 3.6 | |—–|—–| | 2.2 | 6.42| | 3.1 | 3.6 | | 3.5 | 32.6 |
Print the selected value in the console using indexes. Modify the program so that the user can indicate which element of the table they want to read.
forexcercise.exe
Create a program that prints numbers from 0 to 20.
Create a program that prints numbers from 5 to 25.
Create a program that prints numbers from 20 to 0.
Create a program that prints starting from 1 every 3rd number up to 50.
Create a program that prints 100 numbers divisible by 5.
printtable.exe
Create a program that prints the table from the previous task in the console using a loop in the format:
| 3.1 | 3.6 |
| 2.2 | 6.42 |
| 3.1 | 3.6 |
| 3.5 | 32.6 |
task4.exe
Using break and continue instructions, write a program that prints numbers from 0 to a user-specified value, ignoring numbers divisible by 3.
otherloops.exe
Try to complete the previous task using another loop (if you used for using while
or do-while
loops).
primenumbers.exe
Create a program that prints consecutive prime numbers.*
twodimensionarray.exe
Write a program that stores values entered by the user in a 3x3 array.
Add a menu to the program with the following options:
The program should run until the user selects option 4. In cases 1,2,3, it should allow the user to select a row/column and then perform the appropriate calculations.
test checking the value of the average