program executing by negative number

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 NeeZaaR  Icon User is offline

  • New D.I.C Head

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

program executing by negative number

Posted 11 December 2012 - 06:21 AM

Hello everyone.
I have problem the program executing by negative number.
I don't know why?
can you show me what the wrong?


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


typedef struct mycandidate{
    char name[20];
    int votes;
    float percentage;
} candidate;

#define number 4
void get_candidate(candidate list[]);
float count_percentage(candidate list[]);
void print_result(candidate list[],int,int);
int total(candidate list[]);
int winner(candidate list[]);


void main(){
	int index=0,total1,winner1;
	candidate list[50];
    get_candidate(list);
	total1 = total(list);
	count_percentage(list);
	winner1 = winner(list);
	print_result(list,total1,winner1);
}


int winner(candidate list[]){
	int highest,ans=0,i;
	highest = list[0].votes;
	for (i=0;i<number;i++){
		if (list[i].votes > highest){
			highest = list[i].votes;
			ans = i;
		}	
	}
	return ans;
}

int total(candidate list[]){
	int sum=0,i;
	for (i=0;i<number;i++)	{
	sum = sum + list[i].votes;
	}
	return sum;
}

void get_candidate(candidate list[]){
    int i,index=0;
	for (i=0;i<3;i++){
		printf("Candidate %d\n",i+1);
		printf("Enter the name: \n");
    	scanf("%s",&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");
        index++;
    }
}

float count_percentage(candidate list[]){
    float temp=0.0;
    int i;
    for (i=0;i<number;i++){
        temp = temp + (list[i].votes);
    }

    for(i=0;i<number;i++){
		list[i].percentage = ((list[i].votes)/temp) * 100.0;
    }
	return 0;
}

void print_result(candidate list[],int total,int winner){
    int i;
	printf("\n%-15s %-15s %-15s\n", "Candidate", "Votes Received", "% of Total Votes");
    for (i=0;i<number;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);
}




Is This A Good Question/Topic? 0
  • +

Replies To: program executing by negative number

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,462
  • Joined: 25-December 09

Re: program executing by negative number

Posted 11 December 2012 - 07:16 AM

First you need to be more specific as to the problem with your program. Show a sample of your input and then show what output your program is producing, and what you expect your program to produce.

Next you should probably increase your compiler warning level here are the warnings and errors I received when I tried to compile your code:

Quote

main.c|20|error: return type of ‘main’ is not ‘int’ [-Wmain]|
main.c||In function ‘main’:|
main.c|21|warning: unused variable ‘index’ [-Wunused-variable]|
main.c||In function ‘get_candidate’:|
main.c|56|warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat]|
main.c||In function ‘print_result’:|
main.c|81|warning: declaration of ‘total’ shadows a global declaration [-Wshadow]|
main.c|43|warning: shadowed declaration is here [-Wshadow]|
main.c|81|warning: declaration of ‘winner’ shadows a global declaration [-Wshadow]|
main.c|31|warning: shadowed declaration is here [-Wshadow]|
||=== Build finished: 1 errors, 6 warnings ===|


The shadow warnings are because you are trying to use variables with the same names as you have used in your functions. You should never use variable names that conflict with your function names, you will confuse your compiler. It may have problem determining if you are trying to use the function or the variable.

The format warning should be treated as in error and fixed. You need to pass the address of your variable to scanf(), you are trying to pass a pointer to the address of your variable. You don't need the ampersand when dealing with arrays, since the name of the array is the address.

The first warning is telling you main should be defined as returning in int, not void, and you should return an int from this function. The C standard requires that main return an int in a hosted environment.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 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 11 December 2012 - 07:53 AM

I used pointer but this time give me fatal error LNK1120: 5 unresolved externals;
how to solve this problem?


#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 **);
	int total(candidate list[],int **);
	float count_percentage(candidate list[],int **);
	int winner(candidate list[],int **);
	void print_result(candidate list[],int,int,int **); 
	 
	int main(){
	    int total1,winner1;
		int *index=0;
	    candidate list[50];
	    get_candidate(list,&index);
	    total1 = total(list,&index);
	    count_percentage(list,&index);
	    winner1 = winner(list,&index);
	    print_result(list,total1,winner1,&index);
		return 0;
	}
	 
	//-----------------------------------------
	
	void get_candidate(candidate list[],int &index){
	    int i;

	    for (i=0;i<3;i++){
        printf("Candidate %d\n",i+1);
	        printf("Enter the name: \n");
	        scanf("%s",&list[i].name);
	 
	if (strcmp("Done",list[i].name) == 0 || strcmp("Finish",list[i].name) == 0){
	            break;
	            }
	        printf("Enter the index of votes:\n");
	        scanf("%d",&list[i].votes);
	        printf("\n");
	        index++;
	    }
	}

		//-----------------------------------------
	 
	int total(candidate list[],int &index){
	    int sum=0,i;
    for (i=0;i<index;i++)   {
    sum = sum + list[i].votes;
	    }
	    return sum;
	}


		//-----------------------------------------
	
	float count_percentage(candidate list[],int &index){
    float temp=0.0;
	    int i;
	    for (i=0;i<index;i++){
	        temp = temp + (list[i].votes);
	    }
	 
	    for(i=0;i<index;i++){
	        list[i].percentage = ((list[i].votes)/temp) * 100.0;
	    }
	    return 0;
	}



	  //-----------------------------------------


	int winner(candidate list[],int &index){
	    int highest,ans=0,i;
	    highest = list[0].votes;
	    for (i=0;i<index;i++){
	        if (list[i].votes > highest){
	            highest = list[i].votes;
	            ans = i;
	        }  
	    }
    return ans;
	}



	//-----------------------------------------
	 
	void print_result(candidate list[],int total,int winner,int &index){
	    int i;
	    printf("\n%-15s %-15s %-15s\n", "Candidate", "Votes Received", "% of Total Votes");
	    for (i=0;i<index;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
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,462
  • Joined: 25-December 09

Re: program executing by negative number

Posted 11 December 2012 - 08:09 AM

Is this a C or C++ program?

In C there is no such thing as references. The following function implementation would be incorrect:
 void get_candidate(candidate list[],int &index)


I recommend you study the function tutorials supplied in my signature. They should help you better understand how to create and call functions.

You still need to fix the problems I mentioned in my last post.
            scanf("%s",&list[i].name);

This is still wrong, re-read my prior post.

Jim

This post has been edited by jimblumberg: 11 December 2012 - 08:11 AM

Was This Post Helpful? 0
  • +
  • -

#5 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 11 December 2012 - 08:46 AM

this is c not c++
because when i doing like this

int total(candidate list[],int *index)



it will come error..
i am confusing now >_<
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,462
  • Joined: 25-December 09

Re: program executing by negative number

Posted 11 December 2012 - 08:59 AM

Then post the code that is causing the errors, along with the complete error messages exactly as they appear in your development environment. These messages have important information to aid in locating and fixing the errors.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 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 15 December 2012 - 02:06 AM

I have problem with the output it gave zero..
when i type give me zero..
i need know where is the problem exactly and what i should be do ..



#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[]);
	float count_percentage(candidate list[],int);
	void print_result(candidate list[],int,int);
	int main(){
	    candidate list[50];
	    get_candidate(list);
	    return 0;
	}
	void get_candidate(candidate list[]){
	    int i=0,numCandidates=0;
	   
	    for (i=0;i<10;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 =list[i].votes; 
		count_percentage(list,numCandidates);
	}

	float count_percentage(candidate list[],int numCandidates){
	    float temp=0.0;
		int i=0;
	    for (i=0;i<numCandidates;i++)
		{temp = temp + (list[i].votes);}

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


	void print_result(candidate list[],int numCandidates,int temp){
		int highest,iplus=0,i=0;
		highest = list[0].votes;
	    for (i=0;i<numCandidates;i++)
		{
			if (list[i].votes > highest)
				{
					highest = list[i].votes;
					iplus = i;
				}
	
		}

	    printf("\n%-12s %-12s %-12s\n", "Candidate", "Votes Received", "% of Total Votes");
	    for (i=0;i<numCandidates;i++)
			
		{printf("%-12s %-12d %-12.2f\n",list[i].name,list[i].votes,list[i].percentage);}
	    printf("%-12s %-12d\n", "Total", temp);
	    printf("\nThe Winner of the Election is %s\n",list[iplus].name);
	}


Was This Post Helpful? 0
  • +
  • -

#8 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 15 December 2012 - 03:58 AM

Stick with this version:


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

 
int get_candidate(candidate list[]);
void count_percentage(candidate list[],int);
int total(candidate list[],int);
int winner(candidate list[],int);
void print_result(candidate list[],int,int,int);
 
int main(){
    int numCandidates=0,total1,winner1;
	 candidate list[50];
    numCandidates = get_candidate(list);
    total1 = total(list,numCandidates);
    count_percentage(list,numCandidates);
    winner1 = winner(list,numCandidates);
    print_result(list,total1,winner1,numCandidates);
    return 0;
}
 
 
int get_candidate(candidate list[]){
    int i,num;
    printf("Enter the Number of Candidates: ");
    scanf("%d",&num);
    for (i=0;i<num;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");
    }
   return num;
}
 
int total(candidate list[],int numCandidates){
    int sum=0,i;
    for (i=0;i<numCandidates;i++)    {
        sum = sum + list[i].votes;
                            }
    return sum;
}
 
 
void count_percentage(candidate list[],int numCandidates){
    float temp=0.0;
    int i;
    for (i=0;i<numCandidates;i++){
        temp = temp + (list[i].votes);
    }
    for(i=0;i<numCandidates;i++){
        list[i].percentage = ((list[i].votes)/temp) * 100.0;
    }
}
 
 
 
int winner(candidate list[],int numCandidates){
    int highest,iplus=0,i;
    highest = list[0].votes;
    for (i=0;i<numCandidates;i++){
        if (list[i].votes > highest){
            highest = list[i].votes;
            iplus = i;
        }  
    }
    return iplus;
}
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);
}



Don't change this, OK? Copy it.
Was This Post Helpful? 1
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,462
  • Joined: 25-December 09

Re: program executing by negative number

Posted 15 December 2012 - 06:27 AM

Please don't open a new topic for an existing problem, merged topics.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 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 15 December 2012 - 07:59 PM

Thanks Adak
but the problem my lecture order special thing..
like
a. int main()
b. void get_candidates()
c. float count_percentage()
d. void print_result()


i didn't see her note unless yesterday for that for that reason i changed all and make it different.

#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[]);
	float count_percentage(candidate list[],int);
	void print_result(candidate list[],int,int);
	int main(){
	    candidate list[50];
	    get_candidate(list);
	    return 0;
	}
	void get_candidate(candidate list[]){
	    int i=0,numCandidates=0;
	   
	    for (i=0;i<10;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 =list[i].votes; 
		count_percentage(list,numCandidates);
	}

	float count_percentage(candidate list[],int numCandidates){
	    float temp=0.0;
		int i=0;
	    for (i=0;i<numCandidates;i++)
		{temp = temp + (list[i].votes);}

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


	void print_result(candidate list[],int numCandidates,int temp){
		int highest,iplus=0,i=0;
		highest = list[0].votes;
	    for (i=0;i<numCandidates;i++)
		{
			if (list[i].votes > highest)
				{
					highest = list[i].votes;
					iplus = i;
				}
	
		}

	    printf("\n%-12s %-12s %-12s\n", "Candidate", "Votes Received", "% of Total Votes");
	    for (i=0;i<numCandidates;i++)
			
		{printf("%-12s %-12d %-12.2f\n",list[i].name,list[i].votes,list[i].percentage);}
	    printf("%-12s %-12d\n", "Total", temp);
	    printf("\nThe Winner of the Election is %s\n",list[iplus].name);
	}




now i need know how to pass values to print_result because i didn't get any correct output ..
Adak thanks for what you did to me before and i am so sorry for that ..
Was This Post Helpful? 0
  • +
  • -

#11 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: program executing by negative number

Posted 15 December 2012 - 09:08 PM

Is the number of candidates equal to a number of votes?

33        numCandidates =list[i].votes; 




Calling count_percentage inside get_candidate is slightly confusing. Would it not be better to return the number of candidates to the main function and call count_percentage from there?

To return the data you could use either of two options.

/* use the return statement to return the value */
int  get_candidate(candidate list[]);

/* use a reference parameter - pointer */
void get_candidate(candidate list[], int *size);


Was This Post Helpful? 0
  • +
  • -

#12 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 15 December 2012 - 10:50 PM

I am also confuse already from my code ^__^
can you show me how please ?
Was This Post Helpful? 0
  • +
  • -

#13 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: program executing by negative number

Posted 15 December 2012 - 11:22 PM

You can return the number of candidates (number of elements in the list) with :

/* function prototype */
int  get_candidate(candidate list[]);


/* function implementation */
int  get_candidate(candidate list[])
{
  /* ... */

  return numCandidates;  
}




In main a variable stores the returned value.

/* calling function in main */
int main()
{
    int size;
    candidate list[50];

    size = get_candidate(list);

    /* print to check the returned value */
    printf("The number of entered candidates is %d \n", size);

    return 0;
}




In get_candidate a loop is used to enter the data and a counter i is used to enter each candidate.
If Done is entered straight away, then after the loop i will be 0, ie the number of candidates will be 0. So i directly corresponds to numCandidates and could be replaced by it.

C Tutorial
Was This Post Helpful? 0
  • +
  • -

#14 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 15 December 2012 - 11:24 PM

Ah! I thought you were messing with me! < LOL >

Try this:

#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);
int total(candidate list[],int);
int winner(candidate list[],int);
void print_result(candidate list[],int,int,int);
 
int main(){
    int i,numCandidates=0,total1,winner1;
    float percentage=0.0;
	 candidate list[50];

    get_candidate(list,&numCandidates);
    total1 = total(list,numCandidates);

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

    winner1 = winner(list,numCandidates);
    print_result(list,total1,winner1,numCandidates);
    return 0;
}
 
void get_candidate(candidate list[],int *numCandidates){
    int i,num;
    printf("Enter the Number of Candidates: ");
    scanf("%d",&num);
    for (i=0;i<num;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=num;
}
 
int total(candidate list[],int numCandidates){
    int sum=0,i;
    for (i=0;i<numCandidates;i++)    {
        sum = sum + list[i].votes;
                            }
    return sum;
}
 
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;
}
 
int winner(candidate list[],int numCandidates){
    int highest,iplus=0,i;
    highest = list[0].votes;
    for (i=0;i<numCandidates;i++){
        if (list[i].votes > highest){
            highest = list[i].votes;
            iplus = i;
        }  
    }
    return iplus;
}
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
  • +
  • -

#15 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 - 02:03 AM

The above is the correct tweak to your program. Neezaar. It has the correct return types for each function, you specified.

The one you had was chopped up - especially in main(). I don't know how you got that program, but throw it away, and get the above version, again.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2