Question

In: Computer Science

Code should be written in C Use pointer variables for parameter passing, where appropriate and pointer...

Code should be written in C

Use pointer variables for parameter passing, where appropriate and pointer arithmetic when working with arrays.

1) Using initialization lists, create 5 one-dimensional arrays, one for each of these:

  • hold the names of the people running in the election
  • hold the names of the subdivision
  • hold the vote counts in the Brampton subdivision for each candidate
  • hold the vote counts in the Pickering subdivision for each candidate
  • hold the vote counts in the Markham subdivision for each candidate

Use the data from the example below.

* Your C program should take the data in the arrays and produce the output below, neatly formatted as shown:

Candidates                            Subdivisions                 Totals

                             Brampton  Pickering Markham        

Aubrey                     600            800          800            2200

Blake 700            700          600            2000

Chubbs 800           700          800           etc

Oliver 400            450         300             

Zamier 900            900       900            

Totals                     3400            etc                              etc

  1. Create a function that will find the total votes in one subdivision and return that to main( ). Call this function 3 times to produce the 3 totals in the last line of output.
  2. Create a function that will find the total votes for one candidate and return that to main( ). Call this function 5 times to produce the 5 totals in the last column of output. Be very careful with the parameter list here. Send the function only the information it needs.
  3. Create a function that will output the first 2 lines of headings.

Solutions

Expert Solution

Executable code for the given problem is:

#include <stdio.h>
#include <stdlib.h>
void layout()
{
   //printing first two lines of output
   printf("candidates\t\tsubdivisions\t\ttotal");
   printf("\n\t\tbrampton pickering zamier");
  
}
int total_votes_sub(int *a)
{
   int i,t=0;
   for(i=0;i<5;i++)
   {
       /*finding sum of all the votes in a subdivision*/
       t=t+*(a+i);
   }
   return t;  
}
int total_votes_cd(int *a,int *b,int *c,int i)
{
   int t=0;
   /*finding all votes of single candidate from all subdivisions*/
   t=*(a+i)+*(b+i)+*(c+i);
   return t;
  
}
int main()
{
   /*declearing and initializing required variables*/
   int total_votes_brampton,total_votes_pickering,total_votes_zamier,i;
   int total_votes_count[100];
   /*storing names of candidates in names array*/
    char *names[5]={"aubrey","blakes","chubbs","oliver","zamier"};
    char *sub_names[100]={   "Brampton", "Pickering","Markham"};
   int brampton_count[5]={600,700,800,400,900};
   int pickering_count[5]={800,700,700,450,900};  
   int zamier_count[5]={800,600,800,300,900};  
   layout();
   /*finding total votes at each subdivision*/
   total_votes_brampton=total_votes_sub(brampton_count);
   total_votes_pickering=total_votes_sub(pickering_count);
   total_votes_zamier=total_votes_sub(zamier_count);
   for(i=0;i<5;i++)
   {
       /*finding all the votes of every candidate and storing in total_votes_count array*/
       total_votes_count[i] =total_votes_cd(brampton_count,pickering_count,zamier_count,i);
   }
   /*printing according to the requirement*/
   for(i=0;i<5;i++)
   {
       printf("\n%s\t\t%d \t %d\t    %d\t\t%d",names[i],brampton_count[i],pickering_count[i],zamier_count[i],total_votes_count[i]);
   }  
   printf("\ntotals\t\t%d\t %d \t   %d",total_votes_brampton,total_votes_pickering,total_votes_zamier);      
  
}

output of the given code is:


Related Solutions

Solve this problem using pointer variables for parameter passing, where appropriate and pointer arithmetic when working...
Solve this problem using pointer variables for parameter passing, where appropriate and pointer arithmetic when working with arrays Please use the "C" program toProduce the EXACT output shown at the end of the document. 1.Generate a graph that compares, on a month-by-month basis, the monthly rainfall for Kamloops for the first half of 2018 (i.e. Jan – June) versus the normal (30 year average) rainfall for Kamloops for the same months. In main( ), create the 2 data arrays using...
The code should be written in c++. It should be in only OpenGL // ***** ONLY...
The code should be written in c++. It should be in only OpenGL // ***** ONLY OpenGL PLEASE ******// write a program snippet that accepts the coordinates of the vertices of a rectangle centered around the origin, with edges that are parallel to the X and Y axes, and with aspect ratio W and converts it into a rectangle centered around the origin with aspect ratio of 1/W. The program has to figure W from the given points. You MUST...
Explain Parameter passing Semantics in Java/C++
Explain Parameter passing Semantics in Java/C++
C++ Code each of the following functions RECURSIVELY  (each function should use only the parameter value, and...
C++ Code each of the following functions RECURSIVELY  (each function should use only the parameter value, and any local function variables you might need ..no variables are to be declared outside the functions). void upTo(int number) This function prints all values from 1 up to the number. upTo(5); should output: 1 2 3 4 5 HINT: the following functions involve separating a digit from a number. Use of the arithmetic operators % and / are helpful here. Remember a % 10...
Using C++, Write a program that will use pointer syntax to access variables, dynamically allocate memories,...
Using C++, Write a program that will use pointer syntax to access variables, dynamically allocate memories, and pass pointers to functions. 1.            The program should ask the user to enter a size to the array. 2.            The program should dynamically allocate an array with the size. 3.            The program should then ask user to input values to the array 4.            The program should then find the maximum, display all elements forward and reversed using two different ways of pointer access...
Using C++ Use dynamic allocation and functions (using pointer variables only) to read the names from...
Using C++ Use dynamic allocation and functions (using pointer variables only) to read the names from the .txt file and sort the names in lexical order Grumpy Dopey Doc Happy Bashful Sneezy Sleepy
All Code should be written in C: 1. Write a C program which prompts the user...
All Code should be written in C: 1. Write a C program which prompts the user to enter two integer values. Your program should then print out all numbers between 1 and 1000 that are divisible by both of those numbers. 2. Modify your program from question 1 such that the first 1000 numbers that are divisible by both numbers are printed out, instead of numbers up to 1000. 3. Using dynamic memory, allocate memory for an array of 100...
All Code should be written in C: 1. A perfect number is defined as a number...
All Code should be written in C: 1. A perfect number is defined as a number whose proper divisors (factors not including the number itself) add up to the same number. For example, 28 is a perfect number because its perfect divisors are 1, 2, 4, 7, 14, which add up to 28. Write a C function called is_perfect that takes a since integer as input, and returns 1 if the number is perfect, or 0 otherwise. 2. Using the...
I have written code in C programming that checks where the command line arguments are floats...
I have written code in C programming that checks where the command line arguments are floats or not. For example, if I type "./math 1 1 0 0 2.5 3" in the terminal, my program realizes they are all floats but if I type "./math 1 1 0 0 2.5 g", it recognizes that not all arguments are floats and gives an error message. I want to take my code further such that after typing in "./math 1 1 0...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT