program executing by negative number

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1692 Views - Last Post: 16 December 2012 - 06:49 AM Rate Topic: -----

#16 NeeZaaR  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 29-August 12

Re: program executing by negative number

Posted 16 December 2012 - 03:56 AM

thank very much your code was useful to me i changed as i say before to you ..
but still last thing doesn't work is when pass winner it will give me nothing ..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
 
typedef struct mycandidate{
    char name[20];
    int votes;
    float percentage;
} candidate;

 
void get_candidate(candidate list[], int *);
float count_percentage(candidate list[],int);
void print_result(candidate list[],int,int,int);
 
int main(){
    int numCandidates=0,total,winner,sum=0,highest,i;
    float percentage=0.0;
	candidate list[50];

    get_candidate(list,&numCandidates);

    for (i=0;i<numCandidates;i++)    
	{sum = sum + list[i].votes;}
    total = sum;

    percentage=count_percentage(list,numCandidates);
    for(i=0;i<numCandidates;i++)
	{list[i].percentage = ((list[i].votes)/percentage) * 100.0;}

    highest = list[0].votes;
    for (i=0;i<numCandidates;i++)
	{
        if (list[i].votes > highest)
		{
            highest = list[i].votes;
            winner = i;
        } 
	}
    print_result(list,total,winner,numCandidates);
    return 0;
}
 
void get_candidate(candidate list[],int *numCandidates){
    int num,i;
    for (i=0;i<500;i++){
        printf("Candidate %d\n",i+1);
        printf("Enter the name: \n");
        scanf("%s",list[i].name);      //remove the & from in front of "list[i].name"
        if (strcmp("Done",list[i].name) == 0 || strcmp("Finish",list[i].name) == 0)
            {break;}
        printf("Enter the number of votes:\n");
        scanf("%d",&list[i].votes);
        printf("\n");
    }
    *numCandidates=i;
}
 
 
float count_percentage(candidate list[],int numCandidates)
{
    float temp=0.0;
    int i;
    for (i=0;i<numCandidates;i++)
	{temp = temp + (list[i].votes);}
    return temp;
}
 

void print_result(candidate list[],int total,int winner,int numCandidates){
	int i;
    printf("\n%-15s %-15s %-15s\n", "Candidate", "Votes Received", "% of Total Votes");
    for (i=0;i<numCandidates;i++)
		{printf("%-15s %-15d %-15.2f\n",list[i].name,list[i].votes,list[i].percentage);}
    printf("%-15s %-15d\n", "Total", total);
    printf("\nThe Winner of the Election is %s\n",list[winner].name);
}



Was This Post Helpful? 0
  • +
  • -

#17 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: program executing by negative number

Posted 16 December 2012 - 04:50 AM

What did you change from the last post I made of the program?

Show me the exact input that doesn't work.
Was This Post Helpful? 0
  • +
  • -

#18 NeeZaaR  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 29-August 12

Re: program executing by negative number

Posted 16 December 2012 - 04:54 AM

before was 6 function with main
now i did 4 function with main
when i type last part of output this one
printf("\nThe Winner of the Election is %s\n",list[winner].name);

I can't show output what you think the problem is ?
Was This Post Helpful? 0
  • +
  • -

#19 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: program executing by negative number

Posted 16 December 2012 - 04:59 AM

Well! I have compiled the above code, while reducing the number of candidates to 5,
47	    for (i=0;i<500;i++){    // I did it for 5


And it worked just fine, the candidate with many votes was declared the winner, and the other info was displayed too...

What is your problem specifically? Would you try once again, compile the code above and test it...
Was This Post Helpful? 1
  • +
  • -

#20 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: program executing by negative number

Posted 16 December 2012 - 05:19 AM

You can't set this for 500, unless you're really going to enter 500! Use the code I posted, in place of the 500.

for (i=0;i<500;i++){    //500?? OMG!


Was This Post Helpful? 1
  • +
  • -

#21 NeeZaaR  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 29-August 12

Re: program executing by negative number

Posted 16 December 2012 - 05:41 AM

View PostAKMafia001, on 16 December 2012 - 04:59 AM, said:

Well! I have compiled the above code, while reducing the number of candidates to 5,
47	    for (i=0;i<500;i++){    // I did it for 5


And it worked just fine, the candidate with many votes was declared the winner, and the other info was displayed too...

What is your problem specifically? Would you try once again, compile the code above and test it...


you alright ..
i will break it by this condition
 if (strcmp("Done",list[i].name) == 0 || strcmp("Finish",list[i].name) == 0)
            {break;}


it must work as all
Was This Post Helpful? 0
  • +
  • -

#22 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: program executing by negative number

Posted 16 December 2012 - 06:42 AM

View PostAKMafia001, on 16 December 2012 - 04:59 AM, said:

Well! I have compiled the above code, while reducing the number of candidates to 5,
47	    for (i=0;i<500;i++){    // I did it for 5


Just five? Not 500?

Ah, C'mon!! < ROFL! >

Immediately before the break statement, do:
--i;

Because that is putting in one too many candidates. Not sure if that is the only problem, however.

This post has been edited by Adak: 16 December 2012 - 06:46 AM

Was This Post Helpful? 1
  • +
  • -

#23 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: program executing by negative number

Posted 16 December 2012 - 06:48 AM

Yes, I changed this,
47      for (i=0;i<500;i++){

to this,
for (i=0;i<5;i++){


Why?
Was This Post Helpful? 1
  • +
  • -

#24 NeeZaaR  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 29-August 12

Re: program executing by negative number

Posted 16 December 2012 - 06:49 AM

thanks guys ^__^
by your helping i solved this code ^__^
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2