c program with struct

closed account (yC4jURfi)
Good Day, can someone please help me with my problem. I need to do the exact same program but instead, the codes are need to be used with struct and i'm still new with that one. Thanks in advance!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  #include<stdio.h>

void RunAllParts(int votes[5][4])

{

int i=0,j=0;

printf(" \t\tCandidate\tCandidate\tCandidate\tCandidate\n");

printf("Precinct\t A\t\t B\t\t C\t\t D\n");

for (i=0;i<5;i++)

{

 for(j=0;j<4;j++)

 {

  if(j==0)

   printf("%d\t\t", i+1);

  printf("%d\t\t",votes[i][j]);

 }

 printf("\n");

}

printf("\n\n************************************************************************\n");

int sum[6]={0}, cumalativeSum=0;

char candidateName[] = "ABCD";

for (i=0;i<4;i++)

{

 for(j=0;j<5;j++)

 {

  sum[i] += votes[j][i];

  cumalativeSum += votes[j][i];

 }

}


j=0;

for (i=0; i<4; i++)

{

 printf("\nTotal votes of Candidate %c is %d and Percentage is %.2f ", candidateName[j++], sum[i], (float)sum[i]/cumalativeSum);

}

int maxValueIndices[2]= {

 4, 5

};

printf("\n\n************************************************************************\n");

int winnerFound = 0;

float per = 0.0;


for (i=0; i<4; i++)

{

 per = (float)sum[i]/cumalativeSum;


 if( per > 0.5 )

 {

  printf("\nCandidate %c is a winner having percentage %.2f", candidateName[i], per);

  winnerFound = 1;

 }

 if(sum[i]>sum[maxValueIndices[0]]){

  maxValueIndices[1]= maxValueIndices[0];

  maxValueIndices[0] = i;

 }

 else if(sum[i] > sum[maxValueIndices[1]] && sum[i] != sum[maxValueIndices[0]])

 {

  maxValueIndices[1] = i;

 }

}

if(winnerFound == 0){

 printf("\nCandidate %c got the Highest votes %d", candidateName[maxValueIndices[0]], sum[maxValueIndices[0]]);

 printf("\nCandidate %c got the Second Highest votes %d", candidateName[maxValueIndices[1]], sum[maxValueIndices[1]]);

}

}

int main()

{

int votes[5][4]= {

{192,48,206,37},

{147,90,312,21},

{186,12,121,38},

{114,21,408,39},

{267,13,382,29}

};



RunAllParts(votes);


printf("\n\n************************************************************************\n");

printf("\nRunning Code with candidate C receiving only 108 votes\n\n");

int Newvotes[5][4]= {

{192,48,206,37},

{147,90,312,21},

{186,12,121,38},

{114,21,408,39},

{267,13,382,29}

};


RunAllParts(Newvotes);

}
Hello justinejerome,

The first thing you need to do is decide what information you will want in your struct. I am thinking "candidate" name and maybe an array of the votes for that candidate.

Nice code and good use of blank lines, but you have more blank lines than you need. Compare this to what you have and see the difference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include<stdio.h>

void RunAllParts(int votes[5][4])
{
    int i = 0, j = 0;

    printf(" \t\tCandidate\tCandidate\tCandidate\tCandidate\n");

    printf("Precinct\t A\t\t B\t\t C\t\t D\n");

    for (i = 0; i < 5; i++)
    {
        for (j = 0; j < 4; j++)
        {
            if (j == 0)
                printf("%d\t\t", i + 1);

            printf("%d\t\t", votes[i][j]);
        }

        printf("\n");
    }

    printf("\n\n************************************************************************\n");

    int sum[6] = { 0 }, cumalativeSum = 0;

    char candidateName[] = "ABCD";

    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 5; j++)
        {
            sum[i] += votes[j][i];

            cumalativeSum += votes[j][i];
        }
    }

    j = 0;

    for (i = 0; i < 4; i++)
    {
        printf("\nTotal votes of Candidate %c is %4d and Percentage is %5.2f%%", candidateName[j++], sum[i], (float) sum[i] / cumalativeSum * 100.0);
    }

    int maxValueIndices[2] = { 4, 5};

    printf("\n\n************************************************************************\n");

    int winnerFound = 0;

    float per = 0.0;

    for (i = 0; i < 4; i++)
    {
        per = (float) sum[i] / cumalativeSum;

        if (per > 0.5)
        {
            printf("\nCandidate %c is a winner having percentage %.2f", candidateName[i], per);

            winnerFound = 1;
        }

        if (sum[i] > sum[maxValueIndices[0]])
        {
            maxValueIndices[1] = maxValueIndices[0];

            maxValueIndices[0] = i;
        }
        else if (sum[i] > sum[maxValueIndices[1]] && sum[i] != sum[maxValueIndices[0]])
        {
            maxValueIndices[1] = i;
        }
    }

    if (winnerFound == 0)
    {
        printf("\nCandidate %c got the Highest votes %d", candidateName[maxValueIndices[0]], sum[maxValueIndices[0]]);

        printf("\nCandidate %c got the Second Highest votes %d", candidateName[maxValueIndices[1]], sum[maxValueIndices[1]]);
    }
}

int main()
{
    int votes[5][4] =
    {
        {192,48,206,37},

        {147,90,312,21},

        {186,12,121,38},

        {114,21,408,39},

        {267,13,382,29}
    };


    RunAllParts(votes);


    printf("\n\n************************************************************************\n");

    printf("\nRunning Code with candidate C receiving only 108 votes\n\n");

    int Newvotes[5][4] =
    {
        {192,48,206,37},

        {147,90,312,21},

        {186,12,121,38},

        {114,21,408,39},

        {267,13,382,29}
    };

    RunAllParts(Newvotes);

    return 0;  // <--- May or may not be required in a C program, but makes a good break point for testing.
}

I made a small change to line 44.

Line 47 as short as it is works better as 1 line.

The output I get is:

                Candidate       Candidate       Candidate       Candidate
Precinct         A               B               C               D
1               192             48              206             37
2               147             90              312             21
3               186             12              121             38
4               114             21              408             39
5               267             13              382             29


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

Total votes of Candidate A is  906 and Percentage is 33.77%
Total votes of Candidate B is  184 and Percentage is  6.86%
Total votes of Candidate C is 1429 and Percentage is 53.26%
Total votes of Candidate D is  164 and Percentage is  6.11%

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

Candidate C is a winner having percentage 0.53


Just a suggestion. You can do what you want.

Andy
closed account (yC4jURfi)
Hello Andy,

Thank you for your response , I completely forgot about the thing where I need to know what I want to struct... Is it possible if I want to struct just the no. of precincts and no. of candidates?
winner having percentage 0.53


Oh yes?
Hello justinejerome,

Yes. Anything is possible if the language allows it. The bigger question is it practical?

I think the idea you have in mind would be:
1
2
3
4
5
struct name
{
    int numberOfPrecincts = 5;
    int numOfCandidates = 4;
};

But in the end I see no practical use for this. You could juat as easily make the 2 "int"s constant and just use them in the program.

This would seem to be of more use:
1
2
3
4
5
struct Candidate
{
    char name[20];
    int votes[5];
};

Another question to answer. Will the votes always be hard coded in the program or will it need to read a file at some point?

Either way the 2nd example would be of more use.

Then in "main" create an array of structs where each element in sinfomation about a single candidate.

Andy
closed account (yC4jURfi)
Hey Andy,

I now get where you are going with the one I asked and I see your point, but what would be the change for the rest of the codes in the program if I used your recommended struct? I'm still kinda confused there.
Hello justinejerome,

Before you use a struct and adjust the code. First you need to know where the information is coming from. Is it the keyboard or a file?

The code that you have if a good way to test other parts of the program, but hard coding the array is not the way that the program should work.

In "main" you should start with int votes[5][4];.

Then you would eventually replace that with Candidate candidates[MAX_CANDIDATES] where "MAX_CANDIDATES" is defined as a const variable.

At this point you would either call a function to read a file or call a function to get keyboard input.

First you need to know where the information is coming from then work on putting that information into the array of structs. Until this part is done there is no point on changing the rest of your code until you have something to work with.

Just for testing you might consider a simple print function just to print out the array. Some of the code or concepts of the simple print function can be used in other parts of the program later.

Andy
closed account (yC4jURfi)
Thank you for helping me out, I did it and I used the call a function to get keyboard input. I'm still a little confused but I'll practice my skills in using struct. Thank you again!
Hello justinejerome,

If you have any more problems or questions let me know.

Andy
Topic archived. No new replies allowed.