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...

  • 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....

  • 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...

  • Only used main method please. (Count positive and negative numbers and compute the average, of numbers)...

    Only used main method please. (Count positive and negative numbers and compute the average, of numbers) Write a program that reads an unspecified number of integers , determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number. Display the total as a floating-point number. Display the lowest number in the list. Display the...

  • 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...

  • In C program #include<stdio.h> Write a program to simulate a pick-5 lottery game. Your program must...

    In C program #include<stdio.h> 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...

  • 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",...

  • 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...

  • I need help writing these 2 programs please include all the indents :) In this programming...

    I need help writing these 2 programs please include all the indents :) In this programming challenge you are to create two Python programs: randomwrite.py andrandomread.py. One program, randomwrite.py, is to write a set of random numbers to a file. The second program, randomread.py, is to read a set of random numbers from a file, counts how many were read, displays the random numbers, and displays the total count of random numbers. Random Number File Writer (randomwrite.py) Create a program...

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