Question
c++ program

Exercise #1: Index of the Minimum Write a function main() that prompts the user to input a positive integer n, then calls the
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ program:

#include<iostream>

using namespace std;

// Defining function generate to generate random number

void generate(int* arr , int n){

for(int i = 0; i < n; ++i){

// generating a random number between 11 and 217

arr[i] = (rand() % (217 - 11 + 1) + 11) ;

}

}

// Function to return index and minn value

pair<int , int> indexMin(int* arr , int n){

int index = 0 , minValue;

for(int i = 0; i < n; ++i){

if(arr[i] <= arr[index]){

index = i;

}

}

minValue = arr[index];

pair<int , int> p = {minValue , index};

return p;

}

// Writing main function

int main(){

// Declaring variable to store n

int n;

// Declaring an array to store random array

int arr[100];

// Asking the user to input n

cout << "How many integers: " ;

cin >> n;

// Calling generate function

generate(arr , n);

// Calling indexMin function

pair<int , int> p = indexMin(arr , n);

// Displaying array generated

cout << "The " << n << " random integers are: ";

for(int i = 0; i < n; ++i){

cout << arr[i] << " ";

}

cout << endl;

// dispalying minimum value and its index

cout << "The minimum " << p.first << " was located at index " << p.second << endl;

return 0;

}


SS.CPP 1 #include<iostream> using namespace std; 2 3 4 5 6 7 8 // Defining function generate to generate random number void g

// Writing main function int main() { // Declaring variable to store n int n; // Declaring an array to store random array int

OUTPUT:

How many integers: 9 The 9 random integers are: 183 126 155 36 178 126 48 104 206 The minimum 36 was located at index 3

Add a comment
Know the answer?
Add Answer to:
c++ program Exercise #1: Index of the Minimum Write a function main() that prompts the user...
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
  • C++ Font Exercise #5: Multiples of a Number Write a C++ program that prompts the user...

    C++ Font Exercise #5: Multiples of a Number Write a C++ program that prompts the user to enter three integers, n, m, and k, where n is the lower limit, m is the upper limit of a range of positive numbers, and k is any positive number. The program then prints all multiples of the number k between n and m. Treb Sample input/ output: Charac nter the lower and upper limits: 12 93 Enter the multiple: 7 the multiples...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • write in C++ Exercise#1: Is it a Prime? Write a program that prompts the user to...

    write in C++ Exercise#1: Is it a Prime? Write a program that prompts the user to enter a positive integer in the range [100, 100000]. If the integer is not in the specified range the program prompts the user again. The program prints whether the integer is a prime number or not. Sample input/output nter an integer in the range [10e, 10eeee]: 39 nter an integer in the range [100, 100000]: 120453 Enter an integer in the range [10e, 10e000e]:...

  • question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user...

    question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user to enter the number of times n to roll a six-sided die. The program should print to the screen the iteration of the loop and result for each roll 1 ) How many times would you like to roll? 3 roll 6 6 5 1 2) PE 05b 02 (Flash Cards) Write a counter-controlled program that creates addition problems for an elementary kid to...

  • 1. Write a program called Numbers that a. prompts the user for a file name. b....

    1. Write a program called Numbers that a. prompts the user for a file name. b. reads that file assuming that its contents consist entirely of integers. c. prints the maximum, minimum, sum, count (number of integers in the file), and average of the numbers. For example, if the file numberinput.dat has the following content: 4 -2 18 15 31 27 Your program should produce the following output: csc% java Numbers Enter file name: numberinput.daft Maximum31 Minimum- -2 Sum -...

  • 1. Prepare C++ code that prompts the user for an input integer n, creates a vector...

    1. Prepare C++ code that prompts the user for an input integer n, creates a vector consisting of numbers 0,1,2,…,n-1, runs the STL algorithm random_shuffle on it and then prints out the resulting outcome, i.e. the resulting random permutation. 2. Prepare C++ code that prompts the user for an input integer n, creates a vector consisting of numbers 0,1,2,…,n-1, runs the STL algorithm random_shuffle on it and creates three copies of the resulting outcome (probably 3 .txt files).

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • c++ Exercise #2: Words Count Write a program that prompts the user to enter a sentence,...

    c++ Exercise #2: Words Count Write a program that prompts the user to enter a sentence, then counts and prints the number of words in the sentence. Assume that there is more than one space between words of the sentence. Sample input/output: Enter a sentence: This is a 123test in There are 5 words in " This is a 123test \n"

  • Write a c++ program that does the following: in main() 1. prompts for a student's name...

    Write a c++ program that does the following: in main() 1. prompts for a student's name (with spaces_ 2. prompts for 3 grades 3. Calls a function to calculate the average of the 3 grades - arguments must be passed by value 3. displays the student's name and average grade - example: Fred Smith : 85.5 - the average should be a float displaying 1 digit of precision

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