Write a C program to input ten integers into an array named temp. It should find
ID: 3711048 • Letter: W
Question
Write a C program to input ten integers into an array named temp. It should find the largest and smallest number in the array, output the numbers, and output what element of the array they were located.
After displaying the numbers your program should print the two messages.
The maximum value is:
This is element number ____ in the list of numbers
here is my code so far:
#include <stdio.h>
#include "stdafx.h"
int main()
{
#define temp 10
int user_data[temp];
int i;
int total = 0;
int small;
int large;
int b;
int s;
/*inputing the numbers*/
for (i = 0; i < temp; i++) {
printf("Please enter a number:");
scanf_s("%d", &user_data[i]);
}
small = user_data[0];
large = user_data[0];
/*Displaying the numbers*/
for (i = 1; i < temp; i++)
{
if (user_data[i] < small) {
small = user_data[i];
}
if (user_data[i] > large)
{
large = user_data[i];
}
}
printf(" Largest element is: %d",large);
printf(" Smallest element is: %d", small);
getchar();
getchar();
return 0;
}
Explanation / Answer
#include <stdio.h>
int main()
{
#define temp 10
int user_data[temp];
int i;
int total = 0;
int small,smallIndex;
int large,largeIndex;
int b;
int s;
/*inputing the numbers*/
for (i = 0; i < temp; i++) {
printf("Please enter a number:");
scanf("%d", &user_data[i]);
}
small = user_data[0];
large = user_data[0];
/*Displaying the numbers*/
for (i = 1; i < temp; i++)
{
if (user_data[i] < small) {
smallIndex = i;
small = user_data[i];
}
if (user_data[i] > large)
{
largeIndex = i;
large = user_data[i];
}
}
printf(" Largest element is: %d ",large);
printf("This is element number %d in the list of numbers ", largeIndex);
printf(" Smallest element is: %d ", small);
printf("This is element number %d in the list of numbers ", smallIndex);
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.