Howdy, Stranger!

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

Categories

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

  • quagsirequagsire Member Posts: 37
    [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]

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : [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]
  • Shawn CarterShawn Carter Member Posts: 0

    __ \ http://forcoder.org \ free video tutorials and ebooks about [ Visual Basic .NET R PL/SQL C Objective-C Go Scratch Assembly Ruby PHP JavaScript Delphi C# Swift Perl C++ Java Python MATLAB Visual Basic Dart Alice Rust COBOL FoxPro Transact-SQL Erlang Ada Apex Hack Kotlin Clojure Scala D Prolog Crystal Logo ABAP Fortran Scheme F# VBScript Bash Lua SAS Julia Awk LabVIEW ML Lisp ] ___

Sign In or Register to comment.