Question

This lab continues to work with C++ arrays and introduces vectors 1)     First, most code that works...

This lab continues to work with C++ arrays and introduces vectors

1)     First, most code that works on arrays can solve a problem using a single loop. One problem where this is not true is if you need to check if there are any duplicates in the array.

·In a file called duplicates.cpp write a function hasDups(string strs[], int numStrs) that returns true if there are any duplicates in the names array

·Write main() to simulate reading in a single ranked choice ballot and confirm it is valid

o   A ranked choice ballot means you get to vote for more than one candidate

o   The order of your choices indicates who is your first choice, second choice and so on

o   We will read the “ballot” from cin and only check for duplicate names

o   After reading in the choices, main() should call hasDups() to check for duplicates

o   Assume the ballot has no more than 5 choices

·Here are two sample runs

Enter up to 5 choices for the primary, in order

Biden

Harris

Biden

^Z

Sorry, you have chosen the same person more than once

­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­________________________________________

Enter up to 5 choices for the primary, in order

Warren

Booker

Buttigieg

Sanders

Yang

^Z

Ballot is valid

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


#include <bits/stdc++.h>
using namespace std;

int hasDup(string arr[], int n)
{
set < string > s;
for(int i = 0; i < n; i++)
{
if(s.find(arr[i]) != s.end())
return 1;
else
s.insert(arr[i]);
}
return 0;
}

int main()
{
cout<<"Enter up to 5 choices for the primary, in order";
int n = 5;
  
string arr[n];
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
  
if(hasDup(arr, n))
cout << "Sorry, you have chosen the same person more than once";
else
cout << "Ballot is valid";

return 0;
}

/* In this question, i have used a set to store values of string array to check for duplicates because a set cant have duplicates. So we keep on adding the elements of array into set and check if the element is present or not. If present, then return 1 else return 0.

*/

Add a comment
Know the answer?
Add Answer to:
This lab continues to work with C++ arrays and introduces vectors 1)     First, most code that works...
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++ Lab 1. Read in the contents of a text file up to a maximum of...

    C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...

  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • C++ Programming

    PROGRAM DESCRIPTIONIn this project, you have to write a C++ program to keep track of grades of students using structures and files.You are provided a data file named student.dat. Open the file to view it. Keep a backup of this file all the time since you will be editing this file in the program and may lose the content.The file has multiple rows—each row represents a student. The data items are in order: last name, first name including any middle...

  • Mountain Paths (Part 1) in C++ Objectives 2d arrays Store Use Nested Loops Parallel data structures...

    Mountain Paths (Part 1) in C++ Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e. parallel arrays … called multiple arrays in the zyBook) Transform data Read from files Write to files structs Code Requirements Start with this code: mtnpathstart.zip Do not modify the function signatures provided. Do not #include or #include Program Flow Read the data into a 2D array Find min and max elevation to correspond to darkest and brightest color, respectively Compute the shade of...

  • Lab 6 Instructions Objectives: • Executing and experimenting with programs that handle array related topics such...

    Lab 6 Instructions Objectives: • Executing and experimenting with programs that handle array related topics such as declaration, initialization, storing, retrieving, and processing elements. • Effectively manipulating and using ArrayList objects. • Becoming familiar with the use of arrays as method arguments and how array elements are passed to and returned from the called method. • Mastering the use of various debugging tools available on the Eclipse IDU. Important: EVERY one of the following Activities MUST be in a separate...

  • Spanish Vocabulary Helper For this assignment, we are going to work with adding and removing data...

    Spanish Vocabulary Helper For this assignment, we are going to work with adding and removing data from arrays, linear search, and File I/O. In addition, you will learn to work with parallel arrays This program will assist an English-speaking user to build their vocabulary in Spanish This program will read a file containing a list of words in English and another containing the translation of those words in Spanish. The words and corresponding translations should to be stored in two...

  • Lab 5.1 C++ Utilizing the code from Lab 4.2, replace your Cargo class with a new...

    Lab 5.1 C++ Utilizing the code from Lab 4.2, replace your Cargo class with a new base class. This will be the base for two classes created through inheritance. The Cargo class will need to have virtual functions in order to have them redefined in the child classes. You will be able to use many parts of the new Cargo class in the child classes since they will have the same arguments/parameters and the same functionality. Child class one will...

  • C++ Simple Employee Tracking System

    This assignment involves creating a program to track employee information.  Keep the following information on an employee:1.     Employee ID (string, digits only, 6 characters)2.     Last name (string)3.     First Name (string)4.     Birth date (string as MM/DD/YYYY)5.     Gender (M or F, single character)6.     Start date (string as MM/DD/YYYY)7.     Salary per year (double)Thus you must create a class that has all of this, and get/set methods for each of these fields.  Note: The fields that are designated as string should use the string...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

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