C Programming Past Exam worked solutions
Engineering Computing II (National
University of Ireland Galway)
, lOMoARcPSD|12096575
Write a function in C that takes one argument, an array of 5 elements. Your function should print out the value of
the smallest element in the array.
#include <stdio.h>
int question(int
list[5]); void main()
{
int
list[5];
int i;
int min;
for(i=0;i<5;i++)
{
printf("What is the value in cell %d ? : ", i+1);
scanf("%d", &list[i]);
}
min=question(list);
printf("This is the smallest number in your array: %d\n", min);
system("pause");
}
int question(int list[5])
{
int i=0;
int min=list[1];
for (i=0;i<5;i++)
{
if(list[i]<min)
min=list[i];
}
return min;
}
Write a program in C that reads in 5 floating point numbers that represent the sales figures of 5 sales staff. Once
the 5 numbers have been read in your code should find the largest individual sales. Your code should then use this
value to display all those sales figures which are less than 20% of the highest value.
#include
<stdio.h> void
main()
{
float
sales[5]; int
i;
float most_sales=0;
int compare;
for(i=0;i<5;i++)
{
printf("What is the sales of salesperson %d ? : ",
i+1); scanf("%f", &sales[i]);
}
for(i=0;i<5;i++)
{
if(sales[i]>most_sales)
most_sales=sales[i];