Content of the lesson:
To compute the arithmetic mean of all items you will need two variables. Soucet - the total sum of all items inside the array, prum - variable to store the final value of arithmetic mean. When inserting items into the array you should also add them to the variable soucet (do not forget to also add the current value - soucet=soucet+p[i]). When the array if full you can compute the arithmetic mean - the variable Soucet should be divided by the number of items inside the array ( prum=(float)soucet/n ).
include "stdafx.h"
#define N 10
int _tmain(int argc, _TCHAR* argv[])
{
int p[N];
int soucet;
float prum;
soucet=0;
prum=0;
for (int i=1; i < N; i++)
{
printf("Enter the %d value",i);
scanf("%d",p[i]);
soucet=soucet+p[i];
};
prum=(float)soucet/n;
printf("\n");
printf("Average value of the array is: %.2f",prum);
printf("\n");
printf("Press any key to end the program.");
fflush(stdin);
getchar();
return 0;
}
To write positive numbers only you can use FOR cycle to go through the whole array and write only those values which are positive ( if(p[i]>=0) ). We will use another variable pocet because we want to write all values into one line. This variable will indicate the number of positive values which were found. If the value of pocet is 0 (pocet==0) then we will use this command: printf("Positive values are: %d",p[i]). In case we used the writeln command, all other items would be written on the following line.
If the program writes the second, third, ... number (pocet>0) then only colon and the current value are written - you will separate all values using this way.
#include "stdafx.h"
#define N 10
int _tmain(int argc, _TCHAR* argv[])
{
int p[N];
int pocet;
pocet = 0;
for (int i=1; i < N; i++)
{
printf("Enter the %d value",i);
scanf("%d",p[i]);
soucet=soucet+p[i];
};
for (int i=1; i < N; i++)
{
if(p[i]>=0)
{
if(pocet==0) printf("Positive values are: %d ",p[i])
else printf(', ',p[i]);
pocet=pocet+1;
}
}
if (pocet==0)
{
printf("\nNo value is positive\n");
}
printf("\n");
printf("Press any key to end the program.");
fflush(stdin);
getchar();
return 0;
}
Our task is to create a program which can compute quotient of two numbers. To assign the numbers you should use the function scanf which waits until the enter key is pressed and then saves the value from console inside the variable written in brackets. You should realize that division by zero is not acceptable so you have to check values. You can use while cycle which will ask the user to enter a different number (not zero). User will not get behind this check until he inserts a valid number.
After inserting values, the quotient of a and b is saved into the variable výsledek which has to be of the real data type. When computing a quotient of two numbers you might not get an integer result.
In case you wrote %f when writing the variable to console and not %.2fthe program would write a strange result: vysledek =5.000000(a=10, b=2), but you know that the result should be 5. The number .2 sets the amount of decimal places.
#include "stdafx.h"
#define N 10
int _tmain(int argc, _TCHAR* argv[])
{
int a, b;
float vysledek;
printf("Enter the value a:");
scanf("%d",&a);
writeln("Enter the value b:");
scanf("%d",&b);
if (b==0)
{
while (b==0)
{
printf("Division by zero is not acceptable, enter another number:");
scanf("%d",&b);
}
vysledek=(float)a/b;
printf("The quotient of a and b is: %d/%d = %.2f",a,b,vysledek);
end
else
{
vysledek=(float)a/b;
printf("The quotient of a and b is: %d/%d = %.2f",a,b,vysledek);
}
printf("\n");
printf("Press any key to end the program.");
fflush(stdin);
getchar();
return 0;
}
The principle of this procedure is similar to Bubblesort (which is mentioned in the previous lesson) but we will use FOR cycle instead of WHILE cycle and sort the values from the highest one to the lowest one. We have to compare three numbers so set the value of const to 3. To sort all items you need the FOR cycles to run only twice (n-1 times). To write the result we can use a FOR cycle again.
#include "stdafx.h"
#define N 3
int _tmain(int argc, _TCHAR* argv[])
{
int p[N[;
int i,k,pom;
for i:=1 to n do
begin
writeln('Enter the ',i,' value');
readln(p[i]);
end;
for (k=0; k < n-1; k++)
{
for (i=0; i < n-1; i++)
{
if (p[i+1]>p[i])
{
pom=p[i];
p[i]=p[i+1];
p[i+1]=pom;
}
}
}
for (i=1; i < n; i++)
{
if (i==1) printf("Compared values from the highest one to the lowest one: %d", p[i]);
else
printf(", ",p[i]);
}
printf("\n");
printf("Pro ukonceni programu stisknete libovolnou klavesu");
fflush(stdin);
getchar();
return 0;
}