Author Topic: The programming help thread!  (Read 2201 times)

Dark Fact

  • Hero Member
  • *****
  • Posts: 1147
Re: The programming help thread!
« Reply #60 on: July 23, 2007, 07:46:54 AM »
Quote from: nat
sorry dude, I know I'm lame. I still haven't even looked at it yet. Smile I'll get to it next time, I promise. But it always helps to post the code right away with your problem, cuz usually without code I can only speculate at what's going on.

What was the problem, BTW?
Sorry for the late reply.  The problem I had involved finding a way to add up total deposits, withdrawals, and checks within my functions and then returning those totaled values to the Validate_Choice function.  However, the problem always put out incorrect values.  After re-reading the question, I found out that I wasn't supposed to total up my deposits, withdrawals, and checks within their own functions, but within Validate_Choice. 

Anyways, a friend of mine over MSN messenger was able to point out that problem to me and lo and behold, my problem is fixed.

Nat, chances are I'll be asking for help from my other friend over MSN when it comes to programming in the future.  No offense, but I can't really wait a week or two for you to look at my program and tell me what's wrong.  I need solutions, stat.

If I run into additional problems where I need to ask for further help, I'll let you know. :wink:

Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

  • Hero Member
  • *****
  • Posts: 7085
Re: The programming help thread!
« Reply #61 on: July 23, 2007, 03:51:33 PM »
Quote from: nat
sorry dude, I know I'm lame. I still haven't even looked at it yet. Smile I'll get to it next time, I promise. But it always helps to post the code right away with your problem, cuz usually without code I can only speculate at what's going on.

What was the problem, BTW?

Nat, chances are I'll be asking for help from my other friend over MSN when it comes to programming in the future.  No offense, but I can't really wait a week or two for you to look at my program and tell me what's wrong.  I need solutions, stat.

If I run into additional problems where I need to ask for further help, I'll let you know. :wink:

 :oops: Sorry... Again.

Is this for some kind of class, or something?

Anyway, yeah, I'm always here to help if you need/want it. I don't usually take that long to respond. Really.

Dark Fact

  • Hero Member
  • *****
  • Posts: 1147
Re: The programming help thread!
« Reply #62 on: July 25, 2007, 12:34:40 PM »
No, it wasn't in a class.  It's just your typical C function. :wink:

I'll let you know in the future again if I have trouble. :wink:

Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Dark Fact

  • Hero Member
  • *****
  • Posts: 1147
Re: The programming help thread!
« Reply #63 on: July 25, 2007, 02:00:10 PM »
Okay, I've got another thing here with my program that I need a third opinion on.  It's a program that requires me to read in values using an array and then after all the values have been entered, display the minimum value, maximum value, median, and average.

Although the program works, I'm not too sure about the minimum value procedure.  I had a bug with my program that involved displaying "0.00" for minimum value every time the program finishes.  For instance, I would type in values from 1.00 to 20.00 and then the program would display the values in descending order.  The maximum value is 20.00 while the minimum value is 1.00.  The program always displays 0.00 as minimum value.

I corrected the problem by initializing minimum_income to 1.00.  While it corrected the problem, I'm wondering if this kind of procedure is acceptable? Take a look at the code and tell me what you think:

Code: [Select]
/* Prob 11-5
*  Created by Yuriy Mokriy
*/

#include <stdio.h>

#define HOMES 20                        /* The maximum number of homes surveyed */

main()
{
      float household_incomes[HOMES],   /* The array to store the incomes */   
            maximum_income,             /* The maximum income amount in the survey */
            minimum_income,             /* The minimum income amount in the survey */
            average_income,             /* The average income amount in the survey */
            total_income,               /* The total income amount from the survey */
            median_income;              /* The median income amount in the survey */
      int income_count,                 /* Counts the incomes in the survey */
          limit,                        /* Keeps track of how far to go on a pass */
          pass,                         /* The number of the pass */
          temp;                         /* Temporarily stores income amounts for sorting */
     
      /* Initialization */
     
      total_income = 0.00;
      average_income = 0.00;
      median_income = 0.00;
      maximum_income = 0.00;
      minimum_income = 1.00;
     
      /* Prompt for incomes */
     
      for (income_count = 0; income_count < HOMES; income_count++)
      {
       printf("\nEnter the income amount for home #%d: $", income_count + 1);
       scanf("%f", &household_incomes[income_count]);
       total_income += income_count + 1;
       
       /* Determine maximum income */
       
       if (household_incomes[income_count] > maximum_income)
        maximum_income = household_incomes[income_count];
       
       /* Determine minimum income */
       
       if (household_incomes[income_count] < minimum_income)
        minimum_income = household_incomes[income_count];
      }
       
      /* Calculate median and average income */
     
      average_income = total_income / HOMES;
      median_income = ((household_incomes[10] - 1) + (household_incomes[11] - 1)) / 2;

      /* Sort the array in descending order */
     
      limit = HOMES - 2;
     
      for (pass = 1; pass <= HOMES - 1; pass++)
      {
       for (income_count = 0; income_count <= limit; income_count++)
        if (household_incomes[income_count] < household_incomes[income_count + 1])
        {
         temp = household_incomes[income_count];
         household_incomes[income_count] = household_incomes[income_count + 1];
         household_incomes[income_count + 1] = temp;
        }
        --limit;
      }
     
      /* Display the sorted incomes */
     
      printf("\nThe incomes in decreasing order are as follows:\n");
     
      for (income_count = 0; income_count < HOMES; income_count++)
       printf("$%.2f ", household_incomes[income_count]);
      printf("\n");
       
      /* Display the results */
     
      printf("\nMAXIMUM INCOME: $%.2f", maximum_income);
      printf("\nMINIMUM INCOME: $%.2f", minimum_income);
      printf("\nAVERAGE INCOME: $%.2f", average_income);
      printf("\nMEDIAN INCOME:  $%.2f", median_income);   
      printf("\n");
}

Is it okay to use 1.00 as minimum income or do I need to have it set to 0.00 and find another way to fix the problem?

Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

  • Hero Member
  • *****
  • Posts: 7085
Re: The programming help thread!
« Reply #64 on: July 25, 2007, 04:27:05 PM »
Hmm..

If I understand the spirit of your problem correctly, I don't think your solution is correct.

The reason the program always displayed "0" for minimum income before you initialized it to "1" is because using your code:

   if (household_incomes[income_count] < minimum_income)
        minimum_income = household_incomes[income_count];


...that condition was never true unless you entered a negative income. So minimum_income was never set and retained a value of "0". Follow me?

I think a proper solution would be to initialize "minimum_income" on the first loop through with whatever you happen to enter as an income rather than initializing it before the loop. For example inserting the code:

   if (income_count == 0)
        minimum_income = household_incomes[income_count];


...right before the if statement that checks to see if the current income is lower than the current minimum should initialize the minimum_income variable on the first loop through.

Make sense?

Dark Fact

  • Hero Member
  • *****
  • Posts: 1147
Re: The programming help thread!
« Reply #65 on: July 26, 2007, 06:38:57 AM »
I just gave that a shot.  It works great now. :)

My friend over MSN suggested that I use the initialization method for maximum_income and minimum_income but I felt that this just didn't sit right with me as the program required me to determine maximum and minimum income based on inputted values, not on comparisons with pre-initialized values.

Anyways, thanks for the help.  Arrays have been kicking my ass lately.

Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Dark Fact

  • Hero Member
  • *****
  • Posts: 1147
Re: The programming help thread!
« Reply #66 on: August 16, 2007, 11:12:25 AM »
Got another problem.

I have a program that requires me enter text at the terminal.  After I'm finished entering text, I'm then required to provide totals of each letter instance in the text.  For example, if I have a piece of text saying hahayoulose, the program would have to count up these total letters:

total 'a' = 2
total 'e' = 1
total 'h' = 2
total 'l' = 1
total 'o' = 2
total 's' = 1
total 'u' = 1
total 'y' = 1

All other letters would be read in as 0.  I require an array of counters for each letter in order to add up the values.  The program should not distinguish between uppercase and lowercase.  So, if I type in 'a' or 'A', it would still be added to my variable called 'a_count'.  The program also requires an EOF (end-of-file) character to terminate input.

Here's what I have for my code so far:

Code: [Select]
/* Prob 11-9
*  Created by Yuriy Mokriy
*/

#include <stdio.h>

#define ALPHABET_TOTAL 25                 /* The total number of letters in the alphabet */

main()
{
      char text[ALPHABET_TOTAL],          /* Text inputted by the user */
           letter_count;                  /* array subscript for text */
          /* The following declarations below are the array subscripts for the 'text' variable */
      int a_count = 0,
          b_count = 0,
          c_count = 0,
          d_count = 0,
          e_count = 0,
          f_count = 0,
          g_count = 0,
          h_count = 0,
          i_count = 0,
          j_count = 0,
          k_count = 0,
          l_count = 0,
          m_count = 0,
          n_count = 0,
          o_count = 0,
          p_count = 0,
          q_count = 0,
          r_count = 0,
          s_count = 0,
          t_count = 0,
          u_count = 0,
          v_count = 0,
          w_count = 0,
          x_count = 0,
          y_count = 0,
          z_count = 0;
         
      /* Text input */
     
      printf("\nPlease enter text.  Terminate program by pressing enter: ");
      text[letter_count] = getchar();
     
     while (text[letter_count] = getchar() != EOF)
      {
       for (letter_count = 'a'; letter_count <= 'z'; letter_count++)
       {
        while (text[letter_count] = getchar() != '\n')
        {
          if (text[letter_count] == 'a')
           ++a_count;
          if (text[letter_count] == 'b')
           ++b_count;
          if (text[letter_count] == 'c')
           ++c_count;
          if (text[letter_count] == 'd')
           ++d_count;
          if (text[letter_count] == 'e')
           ++e_count;
          if (text[letter_count] == 'f')
           ++f_count;
          if (text[letter_count] == 'g')
           ++g_count;
          if (text[letter_count] == 'h')
           ++h_count;
          if (text[letter_count] == 'i')
           ++i_count;
          if (text[letter_count] == 'j')
           ++j_count;
          if (text[letter_count] == 'k')
           ++k_count;
          if (text[letter_count] == 'l')
           ++l_count;
          if (text[letter_count] == 'm')
           ++m_count;
          if (text[letter_count] == 'n')
           ++n_count;
          if (text[letter_count] == 'o')
           ++o_count;
          if (text[letter_count] == 'p')
           ++p_count;
          if (text[letter_count] == 'q')
           ++q_count;
          if (text[letter_count] == 'r')
           ++r_count;
          if (text[letter_count] == 's')
           ++s_count;
          if (text[letter_count] == 't')
           ++t_count;
          if (text[letter_count] == 'u')
           ++u_count;
          if (text[letter_count] == 'v')
           ++v_count;
          if (text[letter_count] == 'w')
           ++w_count;
          if (text[letter_count] == 'x')
           ++x_count;
          if (text[letter_count] == 'y')
           ++y_count;
          if (text[letter_count] == 'z')
           ++z_count;
        }
       }
         
       for (letter_count = 'A'; letter_count <= 'Z'; letter_count++)
       {
        while (text[letter_count] = getchar() != '\n')
        {
          if (text[letter_count] == 'A')
           ++a_count;
          if (text[letter_count] == 'B')
           ++b_count;
          if (text[letter_count] == 'C')
           ++c_count;
          if (text[letter_count] == 'D')
           ++d_count;
          if (text[letter_count] == 'E')
           ++e_count;
          if (text[letter_count] == 'F')
           ++f_count;
          if (text[letter_count] == 'G')
           ++g_count;
          if (text[letter_count] == 'H')
           ++h_count;
          if (text[letter_count] == 'I')
           ++i_count;
          if (text[letter_count] == 'J')
           ++j_count;
          if (text[letter_count] == 'K')
           ++k_count;
          if (text[letter_count] == 'L')
           ++l_count;
          if (text[letter_count] == 'M')
           ++m_count;
          if (text[letter_count] == 'N')
           ++n_count;
          if (text[letter_count] == 'O')
           ++o_count;
          if (text[letter_count] == 'P')
           ++p_count;
          if (text[letter_count] == 'Q')
           ++q_count;
          if (text[letter_count] == 'R')
           ++r_count;
          if (text[letter_count] == 'S')
           ++s_count;
          if (text[letter_count] == 'T')
           ++t_count;
          if (text[letter_count] == 'U')
           ++u_count;
          if (text[letter_count] == 'V')
           ++v_count;
          if (text[letter_count] == 'W')
           ++w_count;
          if (text[letter_count] == 'X')
           ++x_count;
          if (text[letter_count] == 'Y')
           ++y_count;
          if (text[letter_count] == 'Z')
           ++z_count;
        }
       }
      }         
      printf("\ntotal a or A: %d", a_count);
      printf("\ntotal b or B: %d", b_count);
      printf("\ntotal c or C: %d", c_count);
      printf("\ntotal d or D: %d", d_count);
      printf("\ntotal e or E: %d", e_count);
      printf("\ntotal f or F: %d", f_count);
      printf("\ntotal g or G: %d", g_count);
      printf("\ntotal h or H: %d", h_count);
      printf("\ntotal i or I: %d", i_count);
      printf("\ntotal j or J: %d", j_count);
      printf("\ntotal k or K: %d", k_count);
      printf("\ntotal l or L: %d", l_count); 
      printf("\ntotal m or M: %d", m_count);
      printf("\ntotal n or N: %d", n_count);
      printf("\ntotal o or O: %d", o_count);
      printf("\ntotal p or P: %d", p_count);
      printf("\ntotal q or Q: %d", q_count);
      printf("\ntotal r or R: %d", r_count);
      printf("\ntotal s or S: %d", s_count);
      printf("\ntotal t or T: %d", t_count);
      printf("\ntotal u or U: %d", u_count);
      printf("\ntotal v or V: %d", v_count);
      printf("\ntotal w or W: %d", w_count);
      printf("\ntotal x or X: %d", x_count);
      printf("\ntotal y or Y: %d", y_count);
      printf("\ntotal z or Z: %d", z_count);
}

The array itself has a capacity of 25 to fit 25 letters of the alphabet.  The problem with the program that I keep having is that it just keeps hanging on me after I finished my input.  I suspect it to be a problem with my EOF, but I 'm not sure how to fix this.  I have a couple of for loops that count the presence of each letter both in lowercase and uppercase with the appropriate accumulator being incremented for each instance found.  If anyone can figure out what's wrong, it would be greatly appreciated. :)

Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Bonknuts

  • Hero Member
  • *****
  • Posts: 3292
Re: The programming help thread!
« Reply #67 on: August 16, 2007, 01:12:38 PM »
Just wanted to point out something - there's no need to write that many if statements for the whole alphabet, let alone two sets for upper and lowercase.

In ASCII you can avoid case sensitive checking, if case is not important. Bit 5 of the byte is set when the character is upper case and clear when the character is lower case.

Code: [Select]
unsigned char get_char;
unsigned int count_array[25]; 

   //note: I usually make my arrays a little bigger then the space I need incase I overwrite another area of memory with an out of bounds pointer
.
.
.
  get_char|=0x60;
That will convert all alphabetical characters to lower case. Use get_char&=0x5F to convert it to upper case.

 You'd probably want to check to see of the input char is within the correct range first:

Code: [Select]


 if( (get_char>0x40 && get_char<0x5b) || (get_char>0x60 && get_char<0x7b) )
  {
      get_char&=0x5F;   //convert to lowercase
      get_char-=0x41;   //subtract offset to beginning char starts at 0
      count_array[get_char]++;   //use the char as an index into your array and increment that specific usage
  } else {
            //error
         }


« Last Edit: August 16, 2007, 05:42:22 PM by Bonknuts »

Dark Fact

  • Hero Member
  • *****
  • Posts: 1147
Re: The programming help thread!
« Reply #68 on: August 16, 2007, 04:39:21 PM »
The chapter examples in my textbook doesn't use specific hexadecimal notation in retrieving character values.  A previous example made use of a for loop to count each letter specifically and increment the accumulators if an instance is found.

The example that you provided may read in the values but what am I supposed to do for output? My question asks to display the number of each letter instance in the inputted text.

Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

Bonknuts

  • Hero Member
  • *****
  • Posts: 3292
Re: The programming help thread!
« Reply #69 on: August 16, 2007, 05:38:32 PM »
hmm.. how about this:

Code: [Select]
  for(int i=0,i<25,i++)
  {
     printf("\n total %s or %s: %d", (i + 0x41), (i + 0x61), count_array[i]);
  }
« Last Edit: August 16, 2007, 05:40:03 PM by Bonknuts »

nat

  • Hero Member
  • *****
  • Posts: 7085
Re: The programming help thread!
« Reply #70 on: August 17, 2007, 04:24:25 AM »
Looks like Bonknuts beat me to this one.

Dark Fact

  • Hero Member
  • *****
  • Posts: 1147
Re: The programming help thread!
« Reply #71 on: August 17, 2007, 08:16:32 AM »
My program crashed. :(

Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!

nat

  • Hero Member
  • *****
  • Posts: 7085
Re: The programming help thread!
« Reply #72 on: August 17, 2007, 09:18:51 AM »
Still not working for you?

I'll look at it as soon as I'm off work.

Bonknuts

  • Hero Member
  • *****
  • Posts: 3292
Re: The programming help thread!
« Reply #73 on: August 18, 2007, 06:14:38 AM »

 What compiler are you using? Knowing your way around the debugger is very import (I'd say a must). You can easily(usually) find out at what point the program is crashing when stepping through the debugger.

 Also another way to debug your could is with process of elimination. Like for instance, cut the program down to:
Code: [Select]
     while (text[letter_count] = getchar() != EOF)
      {
          letter_count++;
      }

 hint: I'd take a look at that test statement.

Dark Fact

  • Hero Member
  • *****
  • Posts: 1147
Re: The programming help thread!
« Reply #74 on: August 19, 2007, 04:33:48 PM »
I've tried modifying the code like this but the program still leaves me hanging:

Code: [Select]
/* Prob 11-9
*  Created by Yuriy Mokriy
*/

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

#define ALPHABET_TOTAL 25                 /* The total number of letters in the alphabet */

main()
{
      unsigned int text[ALPHABET_TOTAL];  /* Array to store all letter instances of the alphabet */
      int letter_count,                   /* Counts the letter instances in the text input */
          char_count = 0;                 /* Counts the letters in the text input */
     
       
      /* Text input */
     
      printf("\nPlease enter text.  Terminate program by pressing enter: ");
      text[char_count] = getchar();

      while (text[char_count] = getchar() != EOF)
      {
       if (text[char_count] = getchar() == EOF)
        break;
       else
       {
        ++char_count;
        while (text[char_count] = getchar() != '\n')
        {
         if (text[char_count] = getchar() == '\n')
          break;
         else
          ++char_count;
        }
       }
      }
     
      for (letter_count = 0; letter_count < ALPHABET_TOTAL; letter_count++)
      {                 
       printf("\n total %c or %c: %d", (char_count + 0x41), (char_count + 0x61), text[char_count]);
     
       if((getchar() > 0x40 && getchar() < 0x5b) || (getchar() > 0x60 && getchar() < 0x7b))
       {
        text[char_count] &= 0x5F;
        text[char_count] -= 0x41;
        text[char_count]++;
       }
       else
       {
        printf("\nError.");
        exit(1);
       }
      }     
       
     
}
The program is really stubborn with that EOF statement.  I've tried inserting a couple of break statements to break the loop but that hasn't worked.  I also tried using a different character count variable.  What am I doing wrong now? :(

Sorry, but I don't see your library card on the books of Ys.  Now, RETURN THEM TO ME!!!