Question

Please post following code with criteria below... please leave as many comments as possible Exam Four:...

Please post following code with criteria below... please leave as many comments as possible

Exam Four: the election

Outcome:

  • Student will demonstrate all core competencies from this class:
    • Program Design (design tools)
    • Variables
    • Decision
    • Error Checking
    • Looping
    • Functions
    • Arrays

Program Specifications:

Let’s pretend that we are tracking votes for the next presidential election. There will be two candidates: Ivanka Trump and Michele Obama.

You can assume that there are fifty states casting votes. You will do not need to deal with the names of the states, you can assume that state 0 = the first state and state 49 = the last state.

This program will have menu system. The menu will at least have the following options:

***********************************************************

** CNN VOTER COLLECTION PROGRAM MAIN MENU **

***********************************************************

  1. Enter Votes from a state for Ivanka Trump
  2. Enter Votes from a state for Michele Obama
  3. Display total votes for each candidate
  4. Display the all votes for a selected candidate in order
  5. List the state number(s) where Trump and Obama received the exact same number of votes
  6. For each candidate display the highest number of votes from any state, the lowest number of votes from any state, the average number of votes from all states.
  7. Election is over, Exit Program

Comments:

When the user presses (1) or (2) from above, the program will ask the user to enter a value for the NEXT state. You can assume that the first time they press (1) from the main menu they were entering votes for state ZERO. The second time for the FIRST state. The user will only be typing a single number (the number of votes for that state). The control of the program will bounce back to the main menu.

Note: The votes are not from a single voter, but from ALL votes cast for that candidate from that state.

When the user selects (3) you will simply show the votes by state for each candidate.

State Trump Obama

0 87876 78877

1 98776 576788

When the user presses (4) sort the votes from low to high and display them.

When the user presses (5) the program will create a statement or statements such as:

Obama and Trump each got 2345 votes from state 8.

When the user presses (6) the program will display by state the lowest number of votes for a candidate from any one state, the average votes by a candidate from any one state.

When the user presses (7)…you figure it out.

Submission Requirements:

Your source code must have a comment header and comments within the code. If you name is not in the comments, you will receive 0 points for this exam. You must provide a well written design tool that matches the code and was generated by a computer application. You must do error checking in at least 2 places within the code. You must have a bubble sort and some type of search function. Everything must be written in functions. Main can only have variables, a loop and a switch.

You must place your design tool in the src folder. You must use Windows compress. You must upload your zipped document to the exam thread.

YOU CANNOT:

  • Use global variables
  • Use the word goto
  • Use the break command outside a case statement
  • Use POINTERS, this is an advanced concept taught in C
  • Use a 2D array, this is an advanced concept taught in C
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the following c program to tracking election by state wise and added necessary comments in the program

Program:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//defines for number of states and
//we can modify as we require
#define TOTAL_STATES 50
#define TRUMP 0
#define OBAMA 1

//function prototypes
void addVotes(int voteCount, int stateNum, int TrumpArray []);
void displayVotes(int TrumpArray [],int ObamaArray []);
void sortDisplayVotes(int Array []);
void displaySameVotes(int rumpArray [],int ObamaArray []);
void displayStatistics(int rumpArray [],int ObamaArray []);

//api to display the menu
void displayMenu()
{
printf("1. Enter Votes from a state for Ivanka Trump\n");
printf("2. Enter Votes from a state for Michele Obama\n");
printf("3. Display total votes for each candidate\n");
printf("4. Display the all votes for a selected candidate in order\n");
printf("5. List the state number(s) where Trump and Obama received the exact same number of votes\n");
printf("6. For each candidate display the highest number of votes from any state, \
number the lowest number of votes from any state, the average number of votes from all states.\n");
printf("7. Election is over, Exit Program\n");
}

//main api
int main()
{
//local variables
int i =0;
int j =0;
int TrumpArray[TOTAL_STATES];
int ObamaArray[TOTAL_STATES];
int inp =-1;
int tmpi=0;
int tmpj=0;
int sortCandi=0;
  
//memset both the arrays using -1 to determine how many states has received
//vote count from user input
memset(TrumpArray, -1, sizeof(TrumpArray));
memset(ObamaArray, -1, sizeof(TrumpArray));
  
//do while loop to show the menu
do
{
displayMenu();
//read the user input
scanf("%d", &inp);
  
//switch case to do the process
switch(inp)
{
//exit the Program when user Enters 7 or other numbers
case 7:
printf("Exiting. . . ");
return -1;
  
case 1:
printf("Enter the votes: ");
scanf("%d", &tmpi);
//read and added vote count to the trump array
addVotes(tmpi, i, TrumpArray);
i++;
break;
  
case 2:
printf("Enter the votes: ");
scanf("%d", &tmpj);
//read and added vote count to the Obama array
addVotes(tmpj, j, ObamaArray);
j++;
break;
  
case 3:
printf("List of states and its count: \n");
//display the vote count for both the candidate
displayVotes(TrumpArray, ObamaArray);
break;
  
case 4:
printf("Sort and display as per votes:\n");
printf("Press 1 for Trump\nPress 2 for Obama\n");
scanf("%d", &sortCandi);
  
if(sortCandi == 1)
//sort and display the vote count for the candidate
sortDisplayVotes(TrumpArray);
  
else if(sortCandi == 2)
sortDisplayVotes(ObamaArray);
else
printf("Enter the right value for candidate to sort based on vote count\n.");
  
break;
  
case 5:
//display the same vote count for each state
displaySameVotes(TrumpArray, ObamaArray);
break;
  
case 6:
//display the high, low vote Count from each state
displayStatistics(TrumpArray, ObamaArray);
break;
  
}
  
}while(inp<=7 && inp >= 1);
  
}

//add Votes to the state using index
void addVotes(int voteCount, int index, int Array [])
{
Array[index] = voteCount;
return;
}


void displayVotes(int TrumpArray [], int ObamaArray [])
{
int i =0;
printf("State Trump Obama\n");
for (i=0; i<TOTAL_STATES; i++)
{
//if the index value is not equal to -1, display the vote Count for the State
if(TrumpArray[i] != -1 && ObamaArray[i] != -1)
printf("%d %10d %10d\n", i, TrumpArray[i], ObamaArray[i]);
}
  
return;
}

void swapIndexValue(int *x, int *y)
{
//swap the value for bubbleSort
int temp = *x;
*x = *y;
*y = temp;
}
  
// bubbleSort function to sort from low to high
void sortDisplayVotes(int Array[])
{
int i=0, j=0, t=0;
// two tempArray to store the State number and vote Count for the respective
//State
int tempArray[TOTAL_STATES];
int tempArrayIndex[TOTAL_STATES];
  
memcpy(tempArray, Array, sizeof(int)*TOTAL_STATES);
  
for(i=0; i<TOTAL_STATES; i++)
{
tempArrayIndex[i]=i;
}
  
for (i = 0; i < TOTAL_STATES-1; i++)
{
  
for (j = 0; j < TOTAL_STATES-1; j++)
{
if (tempArray[j] > tempArray[j+1])
{
swapIndexValue(&tempArray[j], &tempArray[j+1]);
t=tempArrayIndex[j];
tempArrayIndex[j]=tempArrayIndex[j+1];
tempArrayIndex[j+1]=t;
}
}
}
  
printf("State voteCount\n");
for (i=0; i<TOTAL_STATES; i++)
{
if(tempArray[i] != -1)
{
printf("%d %10d\n", tempArrayIndex[i], tempArray[i]);
}
}
}

void displaySameVotes(int TrumpArray [], int ObamaArray [])
{
int i=0, j=0;
int flag=0;
for(;i<TOTAL_STATES; i++)
{
if(TrumpArray[i] != -1 && ObamaArray[i] != -1)
{
//display if the state has equal number of vote count
if(TrumpArray[i]==ObamaArray[i])
{
flag=1;
printf("Obama and Trump each got %d votes from state %d.\n", TrumpArray[i], i);
}
}
}
  
if(!flag)
{
printf("There is no equal vote count for both the candidate.\n");
}
}

void displayStatistics(int TrumpArray [], int ObamaArray [])
{
int i=0;
int low=TrumpArray[i];
int high=TrumpArray[i];
int statelow=0;
int statehigh=0;
int sum=0;
int avg=0;
  
for(i=0; i<TOTAL_STATES; i++)
{
if(TrumpArray[i] == -1)
{
break;
}
//find the low vote count
if(low > TrumpArray[i])
{
low = TrumpArray[i];
statelow = i;
}   
//find the high vote count
if(high < TrumpArray[i])
{
high = TrumpArray[i];
statehigh = i;
}   
//find the average vote count
sum=sum+TrumpArray[i];
}
  
avg=sum/i;
printf("Trump's lowest vote count is %d and in state %d\n", low, statelow);
printf("Trump's highest vote count is %d and in state %d\n", high, statehigh);
printf("Trump's average count from all the state is %d\n", avg);
  
i=0;
low=ObamaArray[i];
high=ObamaArray[i];
statelow=0;
statehigh=0;
sum=0;
avg=0;
  
for(i=0; i<TOTAL_STATES; i++)
{
if(ObamaArray[i] == -1)
{
break;
}
if(low > ObamaArray[i])
{
low = ObamaArray[i];
statelow = i;
}   
  
if(high < ObamaArray[i])
{
high = ObamaArray[i];
statehigh = i;
}   
sum=sum+ObamaArray[i];
}
  
avg=sum/i;
printf("\n\nObama's lowest vote count is %d and in state %d\n", low, statelow);
printf("Obama's highest vote count is %d and in state %d\n", high, statehigh);
printf("Obama's average count from all the state is %d\n", avg);
}

Output:

1. Enter Votes from a state for Ivanka Trump
2. Enter Votes from a state for Michele Obama
3. Display total votes for each candidate
4. Display the all votes for a selected candidate in order
5. List the state number(s) where Trump and Obama received the exact same number of votes
6. For each candidate display the highest number of votes from any state, number the lowest number of votes from any state, the average number of votes from all states.
7. Election is over, Exit Program
1
Enter the votes: 234
1. Enter Votes from a state for Ivanka Trump
2. Enter Votes from a state for Michele Obama
3. Display total votes for each candidate
4. Display the all votes for a selected candidate in order
5. List the state number(s) where Trump and Obama received the exact same number of votes
6. For each candidate display the highest number of votes from any state, number the lowest number of votes from any state, the average number of votes from all states.
7. Election is over, Exit Program
2
Enter the votes: 300
1. Enter Votes from a state for Ivanka Trump
2. Enter Votes from a state for Michele Obama
3. Display total votes for each candidate
4. Display the all votes for a selected candidate in order
5. List the state number(s) where Trump and Obama received the exact same number of votes
6. For each candidate display the highest number of votes from any state, number the lowest number of votes from any state, the average number of votes from all states.
7. Election is over, Exit Program
1
Enter the votes: 200
1. Enter Votes from a state for Ivanka Trump
2. Enter Votes from a state for Michele Obama
3. Display total votes for each candidate
4. Display the all votes for a selected candidate in order
5. List the state number(s) where Trump and Obama received the exact same number of votes
6. For each candidate display the highest number of votes from any state, number the lowest number of votes from any state, the average number of votes from all states.
7. Election is over, Exit Program
2
Enter the votes: 200
1. Enter Votes from a state for Ivanka Trump
2. Enter Votes from a state for Michele Obama
3. Display total votes for each candidate
4. Display the all votes for a selected candidate in order
5. List the state number(s) where Trump and Obama received the exact same number of votes
6. For each candidate display the highest number of votes from any state, number the lowest number of votes from any state, the average number of votes from all states.
7. Election is over, Exit Program
1
Enter the votes: 45
1. Enter Votes from a state for Ivanka Trump
2. Enter Votes from a state for Michele Obama
3. Display total votes for each candidate
4. Display the all votes for a selected candidate in order
5. List the state number(s) where Trump and Obama received the exact same number of votes
6. For each candidate display the highest number of votes from any state, number the lowest number of votes from any state, the average number of votes from all states.
7. Election is over, Exit Program
2
Enter the votes: 567
1. Enter Votes from a state for Ivanka Trump
2. Enter Votes from a state for Michele Obama
3. Display total votes for each candidate
4. Display the all votes for a selected candidate in order
5. List the state number(s) where Trump and Obama received the exact same number of votes
6. For each candidate display the highest number of votes from any state, number the lowest number of votes from any state, the average number of votes from all states.
7. Election is over, Exit Program
3
List of states and its count:
State Trump Obama
0 234 300
1 200 200
2 45 567
1. Enter Votes from a state for Ivanka Trump
2. Enter Votes from a state for Michele Obama
3. Display total votes for each candidate
4. Display the all votes for a selected candidate in order
5. List the state number(s) where Trump and Obama received the exact same number of votes
6. For each candidate display the highest number of votes from any state, number the lowest number of votes from any state, the average number of votes from all states.
7. Election is over, Exit Program
4
Sort and display as per votes:
Press 1 for Trump
Press 2 for Obama
1
State voteCount
2 45
1 200
0 234
1. Enter Votes from a state for Ivanka Trump
2. Enter Votes from a state for Michele Obama
3. Display total votes for each candidate
4. Display the all votes for a selected candidate in order
5. List the state number(s) where Trump and Obama received the exact same number of votes
6. For each candidate display the highest number of votes from any state, number the lowest number of votes from any state, the average number of votes from all states.
7. Election is over, Exit Program
4
Sort and display as per votes:
Press 1 for Trump
Press 2 for Obama
2
State voteCount
1 200
0 300
2 567
1. Enter Votes from a state for Ivanka Trump
2. Enter Votes from a state for Michele Obama
3. Display total votes for each candidate
4. Display the all votes for a selected candidate in order
5. List the state number(s) where Trump and Obama received the exact same number of votes
6. For each candidate display the highest number of votes from any state, number the lowest number of votes from any state, the average number of votes from all states.
7. Election is over, Exit Program
5
Obama and Trump each got 200 votes from state 1.
1. Enter Votes from a state for Ivanka Trump
2. Enter Votes from a state for Michele Obama
3. Display total votes for each candidate
4. Display the all votes for a selected candidate in order
5. List the state number(s) where Trump and Obama received the exact same number of votes
6. For each candidate display the highest number of votes from any state, number the lowest number of votes from any state, the average number of votes from all states.
7. Election is over, Exit Program
6
Trump's lowest vote count is 45 and in state 2
Trump's highest vote count is 234 and in state 0
Trump's average count from all the state is 159


Obama's lowest vote count is 200 and in state 1
Obama's highest vote count is 567 and in state 2
Obama's average count from all the state is 355
1. Enter Votes from a state for Ivanka Trump
2. Enter Votes from a state for Michele Obama
3. Display total votes for each candidate
4. Display the all votes for a selected candidate in order
5. List the state number(s) where Trump and Obama received the exact same number of votes
6. For each candidate display the highest number of votes from any state, number the lowest number of votes from any state, the average number of votes from all states.
7. Election is over, Exit Program
7
Exiting. . .

Screen Shot:

Output Screen shot:

Add a comment
Know the answer?
Add Answer to:
Please post following code with criteria below... please leave as many comments as possible Exam Four:...
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
  • COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize...

    COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize votes for a local election Specification: Write a program that keeps track the votes that each candidate received in a local election. The program should output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. To solve this problem, you will use one dynamic...

  • It’s almost election day and the election officials need a program to help tally election results....

    It’s almost election day and the election officials need a program to help tally election results. There are two candidates for office—Polly Tichen and Ernest Orator. The program’s job is to take as input the number of votes each candidate received in each voting precinct and find the total number of votes for each. The program should print out the final tally for each candidate—both the total number of votes each received and the percent of votes each received. Clearly...

  • (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

    (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...

  • Java Programing Code only Ragged Array Assignment Outcome: Student will demonstrate the ability to create and...

    Java Programing Code only Ragged Array Assignment Outcome: Student will demonstrate the ability to create and use a 2D array Student will demonstrate the ability to create and use a jagged array Student will demonstrate the ability to design a menu system Student will demonstrate the ability to think Program Specifications: Write a program that does the following: Uses a menu system Creates an array with less than 25 rows and greater than 5 rows and an unknown number of...

  • ***Please give the java code for the below and add comments for different areas for a...

    ***Please give the java code for the below and add comments for different areas for a dummy to understand*** This java program will combine the techniques of handling arrays, decision control statements, and loops. Your program should accomplish the following: Build an array that holds characters (size 35) Load the array with random capital letters (use random generator) Your program should present a menu to the user allowing for the following “actions”. To “search” for a target letter of the...

  • C++ : Please include complete source code in answer This program will have names and addresses...

    C++ : Please include complete source code in answer This program will have names and addresses saved in a linked list. In addition, a birthday and anniversary date will be saved with each record. When the program is run, it will search for a birthday or an anniversary using the current date to compare with the saved date. It will then generate the appropriate card message. Because this will be an interactive system, your program should begin by displaying a...

  • CAN SOMEONE PLEASE HELP ME WRITE THIS CODE IN C++, PLEASE HAVE COMMENTS IN THE CODE...

    CAN SOMEONE PLEASE HELP ME WRITE THIS CODE IN C++, PLEASE HAVE COMMENTS IN THE CODE IF POSSIBLE!! General Description: Write a program that allows the user to enter data on their friends list. The list holds a maximum of 10 friends, but may have fewer. Each friend has a name, phone number, and email address. The program first asks the user to enter the number of friends (1-10). You are not required to validate the entry, but you may...

  • Using Java, please create the program for the following prompt. MUST CREATE BUTTONS IN A JFRAME,...

    Using Java, please create the program for the following prompt. MUST CREATE BUTTONS IN A JFRAME, as specified by the prompt! DO NOT use user input of 1, 2, 3 etc. User input must come from clicking the buttons. Please be sure to test your program. Thank you! Write a program that displays three buttons with the names or images of three candidates for public of office. Imagine that a person votes by clicking the button that shows the candidate...

  • Please design, write the code and create the requisite submission files in C++ for the following...

    Please design, write the code and create the requisite submission files in C++ for the following problem: A menu-driven program that offers the user 5 options as follows: Menu: 1. Display array 2. Add up all elements in the array and display that number. 3. Add 1 to each element in the array. 4. Swap the values in row one with the values in row three. 5. Exit In main(), declare and initialize a 2-dimensional array that contains 5 x...

  • C LANGUAGE. PLEASE INCLUDE COMMENTS :) >>>>TheCafe V2.c<<<< #include ...

    C LANGUAGE. PLEASE INCLUDE COMMENTS :) >>>>TheCafe V2.c<<<< #include <stdio.h> int main() { int fries; // A flag denoting whether they want fries or not. char bacon; // A character for storing their bacon preference. double cost = 0.0; // The total cost of their meal, initialized to start at 0.0 int choice; // A variable new to version 2, choice is an int that will store the // user's menu choice. It will also serve as our loop control...

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