Question

I need the following done in C++ formatting and please use while statement instead of for.

Write a program called vectorMismatch.c that that reads 2 vectors of 10 positive integers the prints a count of how many time

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please follow the below code and use comments to better understand the code.

SOURCE CODE:

#include <iostream>
#include <vector>
using namespace std;

//Returns the count of matched elements in the two vectors.
int posMatchCount(vector<int> v1,vector<int> v2)
{
int count=0;
int i=0;
while(i<10)
{
if(v1.at(i)==v2.at(i))
count++;
i++;
}
return count;
}

//Returns the count of mis-matched elements in the two vectors.
int MisMatchCount(vector<int> v1,vector<int> v2)
{
int count=0;
int i=0;
while(i<10)
{
int j=0;
while(j<10)
{
//Positions should not be same. so check for i!=j
if(v1.at(i)==v2.at(j) && i!=j)
count++;
j++;
}
i++;
}
return count;
}

//Testing our functions
int main()
{
vector<int> vector1 (10);
cout<<"Enter Vector 1 of 10 positive numbers: ";
// read 10 values into vector 1
for (int i=0; i<10; i++)
cin>>vector1.at(i);

vector<int> vector2 (10);
cout<<"Enter Vector 2 of 10 positive numbers: ";
// read 10 values into vector 2
for (int i=0; i<10; i++)
cin>>vector2.at(i);
  
int count1=posMatchCount(vector1,vector2);
int count2=MisMatchCount(vector1,vector2);
  
//Print the results
cout<<"Vectors match in "<<count1<<" Positions."<<endl;
cout<<"Vectors mismatch in "<<count2<<" Positions."<<endl;
return 0;
}

=====================================

SCREENSHOT FOR CODE:

=====================================

1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 5 //Returns the count of matched elements in the two vecto

==========

SAMPLE OUTPUT:

Result compiled and executed in 36.497 sac(s Enter Vector 1 of 10 positive numbers: 5 5 5 15 5 2 55 5 Enter Vector 2 of 10 po====================================

   Please comment down here in the comment box if you have any doubts.

   Please hit the LIKE Button if you liked the answer.

====================================

Add a comment
Know the answer?
Add Answer to:
I need the following done in C++ formatting and please use while statement instead of for....
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
  • In C... Write a program to simulate a pick-5 lottery game. Your program must generate and...

    In C... Write a program to simulate a pick-5 lottery game. Your program must generate and store 5 distinct random numbers between 1 and 9 (inclusive) in an array. The program prompts the user for: an integer random seed five distinct integers between 1 and 9 (which are stored in another array) The program then compares the two arrays to determine if they are identical. If the two arrays are identical, then the user wins the game. otherwise the program...

  • 29. (20 points) Write the complete C++ program that implements the following program, as if you...

    29. (20 points) Write the complete C++ program that implements the following program, as if you were turning this in for homework. Your program should have a commented header (your name, etc), comments within the program and a well-built display for the output. Don't forget the pre-processor commands Define an array called nums with a maximum of 20 integers, and fill the array with numbers, recei the keyboard (prompt for the numbers.) In this program, exactly 20 integers will be...

  • Create a new file (in Dev C++) Modify program above to use a vector instead of...

    Create a new file (in Dev C++) Modify program above to use a vector instead of an array. Header comments must be present Prototypes must be present if functions are used Hello and goodbye messages must be shown Vector(s) must be used for implementation Use comments and good style practices ====================================================================================================== #include <iostream> using namespace std; int main() { cout<<"Welcome! This program will find the minimum and maximum numbers in an array."<<endl; cout<<"You will be asked to enter a number...

  • Instructions Write a C++ program that performs the following: Accept a list of floating point values...

    Instructions Write a C++ program that performs the following: Accept a list of floating point values on a single line (separated by spaces). This is vector A. The user may enter as many values as they desire. Accept a second list of floating point values on a single line (separated by spaces). This is vector B. The user may enter as many values as they desire. There is no requirement that the two vectors have the same number of elements....

  • use c++ language, keep it simple i am using code block Exercise#2: Arrays with Random Numbers...

    use c++ language, keep it simple i am using code block Exercise#2: Arrays with Random Numbers Write a program that generates n random numbers as follows: Asks the user to input a positive integer n Asks the user to input the maximum value (say m) for the integers to be generated. Write a program that generates the n numbers in the range 0 to m. The program stores the generated numbers in an array A[ Asks the user to enter...

  • No. 7 7th question i want this code to be done on c++ visual studio without...

    No. 7 7th question i want this code to be done on c++ visual studio without using bool operation. and please try to make as simple as possible. Also i need flowchart for the code thanks. Q7 I need a code programmed on c++ visual studios. Also flowchart of the code. Try to make it simple college level understandable code. thanks. b. wg. Prompt the user to input the value of a double variabler, which stores the radius of a...

  • C++ Write A Program: //Please DONT use stdio.h or fancy things like classes Given the following...

    C++ Write A Program: //Please DONT use stdio.h or fancy things like classes Given the following header: vector<string> split(string target, string delimiter); implement the function split so that it returns a vector of the strings in target that are separated by the string delimiter. For example: split("10,20,30", ",") should return a vector with the strings "10", "20", and "30". Similarly, split("do re mi fa so la ti do", " ") should return a vector with the strings "do", "re", "mi",...

  • The following C++ code has a problem where the loop can exceed the size of the...

    The following C++ code has a problem where the loop can exceed the size of the array if the user inputs too many numbers. #include <iostream> using namespace std; int main() { int nums[20] = { 0 }; int a[10] = { 0 }; cout << a << endl; cout << nums << endl; cout << "How many numbers? (max of 10)" << endl; cin >> nums[0]; for (int i = 0; i < nums[0]; i++) { cout << "Enter...

  • Using basic c++ 2. Count the positive and negative numbers using ***while loop*** • Write a...

    Using basic c++ 2. Count the positive and negative numbers using ***while loop*** • Write a program that reads unspecified number of integers , determines how many negative and positive values have been read. Also calculate total and average. Your program will end with input 0. • Output Enter an integer, the input ends if it is 0: 25 34 -89 72 -35 -67 21 48 0 The number of positives is 5 The number of negatives is 3 The...

  • JAVA - the program should output as follows: Please enter a seed: 2345 Please enter the...

    JAVA - the program should output as follows: Please enter a seed: 2345 Please enter the size of the array: 1 Array size must be greater than 1. Please reenter: 0 Array size must be greater than 1. Please reenter: -1 Array size must be greater than 1. Please reenter: 8 Please choose an option: 1 Print the array 2 Find the average 3 Find the largest element 4 Count how many times 3 occurred 5 Count how many elements...

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