Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Write a C++ program to find the largest and smallest element for the following array of integers {34, 67, 54, 98, 20, 11}. The program must have function_largest and function_smallest which return the largest and smallest value, respectively. You are free to choose how to pass your array to your function.

What I have tried:

Can anybody help?

C++
#include <iostream>
using namespace std;

int main()
{
int array{34,67,54,98,20,11};
Posted
Updated 12-Jul-22 16:19pm
v3
Comments
Richard MacCutchan 12-Jul-22 6:36am    
You have the guides already: create two functions, one to find the largest number, and one to find the smallest.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Start by reading the assignment carefully - it is very detailed as to what it requires you to do!

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Quote:
int array{34,67,54,98,20,11};
That's wrong. Replace it with

C++
int array[] = {34,67,54,98,20,11};


I give you a sensible prototype for function_largest:
C++
int function_largest(int arr[], size_t arr_size);
 
Share this answer
 
Comments
0x01AA 12-Jul-22 14:10pm    
Easy 5
CPallini 12-Jul-22 16:13pm    
:-) Thank you.
Patrice T 15-Jul-22 2:34am    
+5
CPallini 15-Jul-22 2:38am    
Thank you.
Im not good in c++ but maybe the code looks like this

#include <iostream>
using namespace std;

//this will be the number of variables in your array
#define array_size 6


//array in c++ is similar to that in c i think
int array[array_size]={34,67,54,98,20,11};

//this will return the largest number in the array
int function_largest(int Array[],int Size){

    int largest=Array[0];       //set index 0 of the array as the largest
    for(int a=1; a<Size; a++){  //start "a=1" since you initialize the largest variable at index 0 
        if(largest<Array[a]){   //then compare each variable
            largest=Array[a];   //then set the largest if the "if" statement above is true
        }
    }
   return largest;
}

//this will return the smallest number in the array
int function_smallest(int Array[],int Size){

    int smallest=Array[0];
    for(int a=1; a<Size; a++){
        if(smallest>Array[a]){
            smallest=Array[a];
        }
    }
   return smallest;
}

int main(){

//call the functions here to print what you need to display
      cout<<"Largest:"<<function_largest(array,array_size)<<"\n";
      cout<<"Smallest:"<<function_smallest(array,array_size);

return 0;
}
 
Share this answer
 
Comments
Dave Kreskowiak 13-Jul-22 0:07am    
Doing someone's homework assignment for them is never a good idea.
bonezegei 13-Jul-22 1:29am    
Yeah you're right sir. Thank you for the reminder.

and I hope if people find my code they read the comment for them to understand it. Although its very simple code but some people just dont really get things that easy. 4 years back i failed a certain subject with that same algorithm. Programming is a very hard subject specially if you dont have your own computer to test some codes.
merano99 14-Jul-22 18:51pm    
You could improve the program if you used a std::array and called std::sort(). In terms of C++, variables and functions could also be packed in a class.
bonezegei 14-Jul-22 21:47pm    
wow thanks, I just tested the std::sort() function today it's amazing. I just realize Im doing my previous codes the hard way, Im gonna explore more about this. Thanks again.
merano99 15-Jul-22 2:58am    
Glad I could help. You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900