Question
Can you please answer this in complete C++ program.
I. Write a program that reads in an array of five integers al 5] and arses the mray in t fncion largest that rcurns the value of the largest element in the arrav, 2.Write a program that reads in two arrays, a[3] and b[3], and passes them to a function Scalarproduct that returns the scalar product of these arrays; the scalar product of two arrays is
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Question 1:

Here is code:

#include <iostream>

#include <limits>

using namespace std;

int largest(int *array, int SIZE)

{

// set initial default larger data array first element

int larger = array[0];

// loop from 1 to size

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

{

// checking if current element is larger

if (larger < array[i])

larger = array[i];

}

return larger;

}

int main()

{

const int SIZE = 5;

int a[SIZE];

cout << "Enter " << SIZE << " values : " << endl;

// loop from 0 to size and read user input

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

{

cout << "Enter values " << (i + 1) << "# : ";

// check if input is valid or not

while (!(cin >> a[i]))

{

// flush the cin data

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << "Invalid entery, Try again : ";

}

}

int result = largest(a, SIZE);

cout << "The largest value in array : " << result << endl;

return 0;

}

Output:

Enter 5 values: Enter values 1#: 5 Enter values 2# : 9 Enter values 3# : 7 Enter values : 5 Enter values 5#: 1 The largest va

Question 2:

Here is code:

#include <iostream>

#include <limits>

using namespace std;

int Scalarproduct(int *a, int *b, int SIZE)

{

int scalar = 0;

// loop from 0 to size

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

{

// product the number of individual index

scalar += a[i] * b[i];

}

return scalar;

}

int main()

{

const int SIZE = 3;

int a[SIZE], b[SIZE];

cout << "Enter " << SIZE << " values : " << endl;

// loop from 0 to size and read user input

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

{

cout << "Enter values a : " << (i + 1) << "# : ";

// check if input is valid or not

while (!(cin >> a[i]))

{

// flush the cin data

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << "Invalid entery, Try again : ";

}

}

// loop from 0 to size and read user input

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

{

cout << "Enter values b : " << (i + 1) << "# : ";

// check if input is valid or not

while (!(cin >> b[i]))

{

// flush the cin data

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << "Invalid entery, Try again : ";

}

}

int result = Scalarproduct(a, b, SIZE);

cout << "The largest value in array : " << result << endl;

return 0;

}

Output:

Enter 3 values: Enter values a : 1# : 1 Enter values a : 2# : 2 Enter values a : 3# : 1 Enter values b : 1# : 1 Enter values

Add a comment
Know the answer?
Add Answer to:
Can you please answer this in complete C++ program. I. Write a program that reads in...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Program in C language for: 1. Create a C program to read 7 integers...

    Write a Program in C language for: 1. Create a C program to read 7 integers and store them in an array. Next, the program is to check if the array is symmetric, that is if the first element is equal to the last one, the value of the second one is equal to the value of the last but one, and so on. Since the middle element is not compared with another element, this would not affect the symmetry...

  • Please complete in C++ (histogram.cpp) Write a pseudocode and C++ program that reads numbers from an...

    Please complete in C++ (histogram.cpp) Write a pseudocode and C++ program that reads numbers from an array (use another function to initialize the array with random values between 1 and 20 inclusive) and graphs the information in the form of a bar chart or histogram-each number is printed, then a bar consisting of that many asterisks is printed beside the number. Here is a sample run of the program: Element Value 12 Histogram ************ * **** * * * *...

  • Write a MIPS program that reads (with an appropriate prompt) a sequence of 20 integers and...

    Write a MIPS program that reads (with an appropriate prompt) a sequence of 20 integers and stores them in an array, and then calls the following two functions and prints the results in a readable format. The two functions are: smallestLargest: computes the smallest and the largest values in the array. oddEven: computes the number of even integers and the number of odd integers.

  • Write a complete Java program in a file caled Module1Progrom.java that reads all the tokens from...

    Write a complete Java program in a file caled Module1Progrom.java that reads all the tokens from a file named dota.txt that is to be found in the same directory as the running program. The program should ignore al tokens that cannot be read as an integer and read only the ones that can. After reading all of the integers, the program should print all the integers back to the screen, one per line, from the smalest to the largest. For...

  • c++ write a program that reads all values from a text file "data.txt" and stores them in ID array valuesl I. The input process from the file should be terminated when a negative value is detec...

    c++ write a program that reads all values from a text file "data.txt" and stores them in ID array valuesl I. The input process from the file should be terminated when a negative value is detected. (An example of such a file is shown below). Also, the program should copy from values[ 1 any value which has even sum of digits and its index (location) to two new arrays evenArr 1 and idxArrl I, respectively. In addition, the program must...

  • can you comment so that I may understand how it works. 3. Write a C++ program...

    can you comment so that I may understand how it works. 3. Write a C++ program that does the following: Write a function (smallestindex) that has two parameters: an array containing elements of type int and the length of the array. The function returns the index of the array's smallest element. ii. Use the main function to call the smallestindex function

  • Please help :) Problem 1: Complete separately Dr. Rocklino a) Write a program in which the...

    Please help :) Problem 1: Complete separately Dr. Rocklino a) Write a program in which the main method creates two arrays, each with 10 int slots. main initializes the first array to randomly generated integers. It then calls a method named copy, passing it the two arrays. Your copy method copies the contents of the first array to the second array. It then returns to main. main then displays the contents of both arrays. b) Write a program in which...

  • Please solve this C language progrm (2-D Array program) the most basic way ! Thank You!...

    Please solve this C language progrm (2-D Array program) the most basic way ! Thank You! Write a function that takes a 4 x 4 array of integers as its parameter. The function must use two nested for-loops to determine the largest integer in the array as well as its row and column numbers. Recall that C-functions cannot return multiple values. Use pointers in the function parameter list to relay the results to the caller program. Write another function, which...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • Please do in C and only use stdio.h. Write a program that reads n integers into...

    Please do in C and only use stdio.h. Write a program that reads n integers into an array, then prints on a separate line the value of each distinct element along with the number of times it occurs. The values should be printed in ascending order. Turn in your code and input/result together. Suppose, for example, that you input the values -7 3 3 -7 5 5 3 as the elements of your array. Then your program should print -7...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT