Question

6. a) Write C++ code for a loop that simultaneously computes both the maximum and minimum...

6. a) Write C++ code for a loop that simultaneously computes both the maximum and minimum of an array.

b) What is wrong with the following loop?

             int values[10];

             for (int i = 1; i <= 10; i++)

                  {

                     values[i] = i * i;

                  }

Explain two ways of fixing the error.

Homework Answers

Answer #1

Answer 6a:

#include <iostream>

using namespace std;

int main()
{
int arr[]={10,2,5,6,3,9};
int minN=arr[0]; //assuming 0th element as min
int maxN=arr[0];//assuming 0th element as max
for(int i=0;i<6;i++){
//checking if current element is less than min than make current element as min
   if(arr[i]<minN)
       minN=arr[i];
   //checking if current element is greater than max than make current element as max
   if(arr[i]>minN)
       maxN=arr[i];  
}
cout<<"Min element: "<<minN<<endl;
cout<<"Max element: "<<maxN<<endl;


return 0;
}

Answer 6b:

issue indexes of array starts from 0 to length -1 so here we should iterate till 0 to 9

Correct Code:

for (int i = 0; i < 10; i++)

                  {

                     values[i] = i * i;

                  }

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ Code! Represent the following matrix using a two-dimensional array and write a nested for loop...
C++ Code! Represent the following matrix using a two-dimensional array and write a nested for loop to initialize the elements (do not initialize the array in the declaration): 10  9   8 7   6   5 4 3   2
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
Write a function (java static method) that computes the maximum of two integer. maxOfTwo(1, 2) →...
Write a function (java static method) that computes the maximum of two integer. maxOfTwo(1, 2) → 2 maxOfTwo(2, 1) → 2 maxOfTwo(1, 1) → 1 **(to start use): public int maxOfTwo(int a, int b) { And then complete the function (java static method)MaxOfThree so that it returns the maximum (highest) of the three int values passed as the parameters. maxOfThree(1, 2, 3) → 3 maxOfThree(0, 2, 1) → 2 maxOfThree(88, 4, 5) → 88 **(to start use): public int maxOfThree(int...
Expand the code below to display both the value of the loop variable and this value...
Expand the code below to display both the value of the loop variable and this value squared. Put the pairs of values on a new line. #include <stdio.h> #include <stdlib.h> int main(void) { int i; int a; int b; do { printf("Please enter the lower value: "); scanf("%d", &a); printf("Please enter the upper value: "); scanf("%d", &b); if (a>b) printf("The upper value must be greater than the lower value\n\n"); } while (a>b); for(i=a; i<=b; i++) printf("%d ", i); return 0;...
(2nd Problem) From Chapter 15 on page 1080, do problem #5. Recursive function that computes the...
(2nd Problem) From Chapter 15 on page 1080, do problem #5. Recursive function that computes the same of values in an array. To test this function in the main, create the following array: int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; #15. Write a recursive function that finds and returns the sum of the elements of an int array. Also, write a program to test your function.
In this lab, you complete a partially prewritten C++ program that uses an array. The program...
In this lab, you complete a partially prewritten C++ program that uses an array. The program prompts the user to interactively enter eight batting averages, which the program stores in an array. The program should then find the minimum and maximum batting average stored in the array as well as the average of the eight batting averages. The data file provided for this lab includes the input statement and some variable declarations. Comments are included in the file to help...
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the traveling pointer notation to traverse the array. The function has the following prototype. int maximum ( int *p [ ], int n);
Consider the C code below which reverses the elements in the array A in place. Both...
Consider the C code below which reverses the elements in the array A in place. Both array indexing and pointer versions are given. Assume that A holds doubleword integers and that size is the number of elements in the array. Translate to RISC-V using as few instructions as possible. For your RISC-V code, assume the base address of A is initially in x10 and that the number of elements in the array (i.e., size) is initially in x11. long long...
In C++ Please, Write a code fragment that uses a loop to calculate ln (i.e. natural...
In C++ Please, Write a code fragment that uses a loop to calculate ln (i.e. natural log), from values of ranging from ‘1’ through ‘10’ in steps of ‘1’. Print out in this format: Ln(1) = 0 Ln(2) = 0.6931 etc.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT