Topic: Basics of C++ Programming Language "ARRAY IN C++"
Codes discussed will be posted here. Write your conclusion on the ARRAY discussion.
Filename: Ch8_RangeBasedMax.cpp
Code:
--->>https://drive.google.com/file/d/1xgJ6W-PrqmubVcR5sQea4X95PByzWiOb/view?usp=sharing
*Note need proper codes and conclusion on how it's done and about the results. And please give the proper solution and explanation.
Note: Place your conclusion on why did it turn out like that and give your reasons why the results turned like that, one paragraph will do for the explanation and conclusion.
Note: Please place the proper conclusion and explanations and results of the codes which were given.
NOTE: GIVE A CONCLUSION ABOUT THE CODE AND THE RESULTS. EXPLAIN WHAT THE CODE IS ABOUT
#include <iostream>
#include <iomanip>
using namespace std;
/*
The max function receives an array of numbers as variable list.
The variable maxNum is used to extract the largest number from the list.
For, maxNum is assigned the very first element of the array list as maxNum = = list[0].
the pointer p is used to hold the address of array list as * p = &list[0]
A while loop is nused to get the array length.
Further, the array list is traversed to get the largest number using the comparaison method.
*/
int max(int list[]);
int max(int list[])
{
int maxNum = list[0];
int *p = &list[0];
int x,l;
l=0;
while(*(p+l)!='\0') l++;
for(x=0;x<l-1;x++) if (*(p+x) > maxNum) maxNum = *(p+x);
return maxNum;
}
/*
In main function, an array of numbers is created using the variable numList.
The variable l is used to get the array length.
A variable maxNum is used to hold the largest number in the array.
A For loop is nused to traverse the array numList to get the largest number using one to one comparison method.
The largest number is displayed using cout method.
Further, the max function is called with numList as variable passing.
The max function returns the largest number in the main function.
The largest number as obtained form the max function is also displayed using the cout method.
*/
int main()
{
int numList[] = {23, 5, 6, 15, 18, 4, 16, 24, 67,72, 54, 87, 76, 11, 100, 12, 34}; //Line 1
int n,l;
auto int maxNum = numList[0];
l = sizeof(numList)/sizeof(numList[0]);
for (n=0;n<l;n++) if (numList[n] > maxNum) maxNum = numList[n];
cout << "\nThe largest number is : " << maxNum << "\tas obtained within the main function"<<endl;
maxNum = max(numList);
cout << "\nThe largest number is : " << maxNum << "\tas obtained when calling the max function"<< endl;
return 0;
}
/*
Conclusion: The largest number is computed out within the main function and as well as using the max function.
Both results are displayed using the cout method.
In both cases, the largest number is same and it should be.
*/
Comments
Leave a comment