Program Code to print the largerest integer from an array of 10

Can someone please let me know how to include a function that will print the largest and also a code to print the smallest integer from a list of 10 integers that have been input by a user.

Here is what I have so far. Need help really bad!

#include
main()
// Declaration statements
{
int a[10];
int sum = 0;
int reada(int a[]);
int findsuma(int a[]);
int smallesta(int a[]);
int largesta (int a[]);
void printa(int a[]);


reada(a);
sum = findsuma(a);
printa(a);
//Prints the sum to screen
cout <<"The sum is" << sum <<endl;

return 0;
}

int reada(int a[])
{
int i;
for (i=0; i<=9; ++i)
//Prompts the user to enter 10 integers one at a time
{
cout << "Enter an integer" << endl;
cin >> a[i];
}
return (a[0]);
}

int findsuma (int a[])
{
//Adds the 10 integers
int sum =0;
int i;
for (i=0; i<=9; ++i)
sum = sum + a[i];

return (sum);
}

void printa(int a[])
//Prints the list of entered integers
{
int i;
for (i=0; i<=9; ++i)
cout << a[i] <<endl;
}


Comments

  • [code]
    int largesta(int a[])
    {
    int largest = a[0];
    for (int i = 1; i < 10; i++)
    {
    if (a[i] > largest)
    largest = a[i];
    }
    return largest;
    }
    int smallesta(int a[])
    {
    int smallest = a[0];
    for (int i = 1; i < 10; i++)
    {
    if (a[i] < smallest)
    smallest = a[i];
    }
    return smallest;
    }
    [/code]

  • : [code]
    : int largesta(int a[])
    : {
    : int largest = a[0];
    : for (int i = 1; i < 10; i++)
    : {
    : if (a[i] > largest)
    : largest = a[i];
    : }
    : return largest;
    : }
    : int smallesta(int a[])
    : {
    : int smallest = a[0];
    : for (int i = 1; i < 10; i++)
    : {
    : if (a[i] < smallest)
    : smallest = a[i];
    : }
    : return smallest;
    : }
    : [/code]
    :
    :
    [blue]Good code, but if you definitely need both max. and min. you should do it in one 'for()' statement - makes it go faster...[/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories