arrays:
What is a flag?
A flag is an algorithm that informs the program that a certain condition has occured.
example-9
main()
{
int temp;
float celsius;
char repeat;
char flag;
do
{
flag='n";
do
{
if(flag=='n')
printf("Input a valid temperature :");
else
printf("input a valid temperature,stupid:");
scanf("%d",&temp);
flag='y';
}
while (temp<0||temp >100);
celsius=(5.0/9.0)*(temp-32);
printf("%d degrees F is %6.2f degrees celsius\n",temp,celsius);
printf("Do you have another temperature?");
repeat=getchar();
putchar('\n');
}
while (repeat=='y' || repeat=='Y");
}
That was an example of how flags work.
What is the break command?
The break command ends the loop in which it is placed just as if the while condition, or the condition in a for loop becomes false.
How to declare an array?
An array can be defined as follows:
int temp[5]={45,56,12,98,12};
This would mean the following:
temp[0]=45....temp[4]=12
This was a single dimension array with 5 elements of the integer type.If you wanted to depict float variables just use float temp instead of int temp.
Let us now see an example of using an array for two tasks.
main()
{
int temps[31];
int index,total;
float average,celsius;
total=0.0;
for(index=0;index<31;index++)
{
printf("enter temperature #%d:",index);
scanf("%d",&temps[index]);
}
for(index=0;index<31;index++)
total+=temps[index];
average=total/31.0
printf("average is:%f\n\n", average);
puts9"fahrenheit\tcelsius\n");
for(index=0;index<31;index++)
{
celsius=(5.0/9.0)*(temps[index]-32);
printf("%d\t\t%6.2f\n",temps[index],celsius);
}
}
Now I am going to show you how to pass an array. When you pass an array you are actually passing the address of the array.
example-10
#define count 31
main()
{
int temps[count];
int index;
float celsius;
for(index=0; index< count;index++)
{
celsius=(5.0/9.0)*(heat[index]-32);
printf("%d\t\t%6.2f\n",heat[index],celsius);
}
}
No comments:
Post a Comment