Question

In: Computer Science

Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and...

Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and then find the index of the largest element in the array.

You are to write two functions, printArray() and getIndexLargest(), which are called from the main function.

printArray() outputs integers to std::cout with a space in between numbers and a newline at the end.

getIndexLargest () returns the index of the largest element in the array. Recall that indexes start at 0. If there are multiple largest elements, return the smallest index. See the main() to see how the two functions are called.

#include <iostream>
#include <string>

using namespace std;

// Implement printArray here


// Implement getElement here


// DO NOT WRITE ANYTHING BELOW THIS LINE

int main() {
   int myarray[100];
   cout << "Enter number of integers : ";
   int n;
   cin >> n;
   cout << "Enter " << n << " integers" << endl;
   for (int i = 0; i < n; i++)
       cin >> myarray[i];
   cout << "Contents of array : ";
   printArray(myarray, n);
   cout << "Output of getElement: " << getElement(myarray, n) << endl;
}

Solutions

Expert Solution

#include <iostream>
#include <string>

using namespace std;

// Implement printArray here
void printArray(int arr[], int n) {
    for (int i = 0; i < n; ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

// Implement getElement here
int getElement(int arr[], int n) {
    int maxIndex = 0;
    for (int i = 0; i < n; ++i) {
        if (arr[i] > arr[maxIndex]) {
            maxIndex = i;
        }
    }
    return maxIndex;
}

// DO NOT WRITE ANYTHING BELOW THIS LINE

int main() {
    int myarray[100];
    cout << "Enter number of integers : ";
    int n;
    cin >> n;
    cout << "Enter " << n << " integers" << endl;
    for (int i = 0; i < n; i++)
        cin >> myarray[i];
    cout << "Contents of array : ";
    printArray(myarray, n);
    cout << "Output of getElement: " << getElement(myarray, n) << endl;
}


Related Solutions

Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
Problem Definition: Problem: Given an array of integers print all pairs of integers a and b...
Problem Definition: Problem: Given an array of integers print all pairs of integers a and b where a + b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a + b = 16 A= [ 10, 4, 6, 15, 3, 5, 1, 13] The following are pairs that sum to 16: 13, 3 6, 10 15, 1 Your program should print these...
Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
In C Write a program to read a one-dimensional array, print sum of all elements using...
In C Write a program to read a one-dimensional array, print sum of all elements using Dynamic Memory Allocation.
use cpp 1 a)Write a console program which creates an array of size 100 integers. Then...
use cpp 1 a)Write a console program which creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }.  For this part of assignment you should first write a recursive Fib(n) funcion, as was done in class.For testing, print out the 100 integers. 1 b) For second part of this assignment,...
You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The...
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The program then calls the foo function. The function will modify the array x of size n by doubling any values in x that are less than the threshold. The function will count how many values were changed and how many were not changed via two reference parameters. The function signature is: void foo(int n, int x[], int threshold, int *pChanged, int *pUnchanged); The function...
Using C++ code, write a program named q5.cpp to print the minimum of the sums x...
Using C++ code, write a program named q5.cpp to print the minimum of the sums x + y^3 and x^3 + y, where x and y are input by a user via the keyboard.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT