Results 1 to 2 of 2

Thread: C Program - Percentage using pointer

  1. #1
    Join Date
    Apr 2012
    Posts
    1

    C Program - Percentage using pointer

    Hello everyone..
    I'm having a hard time to complete this code.
    Can anyone help me, because I didn't know how to solve for the percentage.
    Thank you

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

    void main()
    {
    char name[50],name2[50];
    char *ptr;
    char **nameptr = NULL;

    int votes;
    int max = 0;
    int *votes_received = NULL;
    float *percent_of_total_votes = NULL;
    int offset = 0; //for name_ptr and vote_received
    int winner = 0; //check the winner (array element in vote_received)
    int i; //for loop
    int total = 0; //total votes

    while (strcmp(name2, "habis") != 0)
    {
    printf("Enter name of candidate (habis to exit):");
    scanf("%s",&name);

    for (i=0;i<=strlen(name);i++)
    name2[i] = tolower(name[i]);

    if (strcmp(name2, "habis") != 0)
    {
    printf("Enter %s's votes:",name);
    scanf("%d",&votes);

    if (nameptr == NULL)
    {
    nameptr = (char**) malloc(sizeof(char**));
    votes_received = (int*) malloc(sizeof(int));

    if (nameptr == NULL)
    {
    printf("Not enough memory 1.\n");
    exit(EXIT_FAILURE);
    }
    }
    else
    {
    nameptr = (char**) realloc(nameptr, (offset + 1) * sizeof(char**));
    votes_received = (int*) realloc(votes_received, (offset + 1) * sizeof(int));

    if (nameptr == NULL)
    {
    printf("Not enough memory 2.\n");
    exit(EXIT_FAILURE);
    }
    }


    ptr = (char*) malloc(strlen(name));
    strcpy(ptr, name);
    nameptr[offset] = ptr;
    votes_received[offset] = votes;
    offset++;
    }


    } //close while loop


    for(i=0;i<offset;i++)
    {
    total = total + votes_received[i];

    percent_of_total_votes[i] = (float)(votes_received[i]/100)*total;
    }


    printf("\n");
    printf("Candidate\tVotes\tPercent\n");

    for(i=0;i<offset;i++)
    {
    printf("%s\t\t",nameptr[i]);
    printf("%d\t",votes_received[i]);
    printf("%.2f\n",percent_of_total_votes[i]);

    if (votes_received[i] > max)
    {
    max = votes_received[i];
    winner = i;
    }
    }

    printf("\nTotal votes : %d\n",total);
    printf("The winner is : %s\n\n",nameptr[winner]);



    }

  2. #2
    Join Date
    Nov 2011
    Posts
    659

    Re: C Program - Percentage using pointer

    You will have to add more element to the table rather than percent_of_total_votes and you will have to reallocate the percent_of_total_votes as well.
    else
    {
    nameptr = (char**) realloc(nameptr, (offset + 1) * sizeof(char**));
    votes_received = (int*) realloc(votes_received, (offset + 1) * sizeof(int));
    ...
    }

    You can try out using this as well (votes_received[i]/total) as both the numbers are integers this will help you out in truncating the decimals , the best way will be casting them altogether.
    percent_of_total_votes[i] = ((float)votes_received[i]/(float)total)*100.0;

Similar Threads

  1. Changing Stylus pointer to normal Pointer in Tablet PC
    By Kelley in forum Portable Devices
    Replies: 3
    Last Post: 21-06-2011, 07:22 PM
  2. Problem in assigning value to pointer in C-program
    By Juany in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 04:19 PM
  3. Differentiation between void pointer and null pointer
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 12:11 PM
  4. What is the Program to calculate the percentage?
    By Ekpah in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 12:30 PM
  5. Percentage of loading
    By Macadrian in forum Software Development
    Replies: 4
    Last Post: 04-11-2009, 07:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,403,879.90444 seconds with 17 queries