Programmimg difficulities

Discussion in 'Programmer's Corner' started by nanobyte, Oct 14, 2004.

  1. nanobyte

    Thread Starter Senior Member

    May 26, 2004
    118
    1
    I am learning C++. I am trying to make a program that prompts the user to enter three decimals and returns the largest of the three. I have a header file and the actual program file. When I try to run the program on Borland C++ Builder 6 it tells me there is an Declaration Synatx error on the 15th line of the program file. Could somebody tell me what the heck is going on! The first attachment is the program file that has the error and the second attachment is the header file that I believe is alright since no errors are shown for it. I'm already behind as it is so can somebody help me! :( :(
     
  2. haditya

    Senior Member

    Jan 19, 2004
    220
    0
    are all variables used declared in the start of the program---i dun knwo much bout c++ but in c one has to declare all varibles used at the start of the program... thats wat we call type declarations
     
  3. Perion

    Active Member

    Oct 12, 2004
    43
    1
    You've got multiple function declarations and definitions all over the place and have mixed up what the header and program files should do. You have two main() definitions (???), and have multiple declaraions for larGest(), called the function from one of the main() functions, but never defined it anywhere.

    Here's a suggestion:

    In your header (.h) file just declare your function protoypes (at least for this particular program), then define those functions in the program file and fix the two main() functions so you have one coherent main().

    Here's an example header:

    Code ( (Unknown Language)):
    1. //***** A header file - badprogrammyheader.h ******
    2. #ifndef __badprogrammyheader_h //Prevents multiple includes
    3. #def __badprogrammyheader_h
    4. //Declare your function prototypes
    5. void myHeader();
    6. double larGest(double, double, double);
    7. #endif
    8. //********* That's it for your header file ********
    And for your cpp program file, maybe something like this:
    Code ( (Unknown Language)):
    1. //
    2. //Include all the headers you need for the program -
    3. //or you could put the standard header includes in your
    4. //badprogrammyheader_h and then just include your header
    5. //in this file.
    6. //
    7. #include <conio.h>
    8. #include <iostream>
    9. #include "badprogrammyheader_h"
    10.  
    11. //Declare any global variables
    12. //(there are none)
    13.  
    14.  
    15. //Define your main() function
    16.  int main()
    17.  {
    18.  double uno;
    19.  double dos;
    20.  double tres;
    21.  
    22.  clrscr();
    23.  myHeader(); // Call your first function
    24.  //
    25.  // Any other code?
    26.  //
    27.  
    28.  
    29.  //Prompt the user
    30.  cout << "Put three numbers in before I buss your head in" <<endl;
    31.  cin >> uno >> dos >> tres;
    32.  
    33.  cout << "The largest of the the three numbers is "
    34.     << larGest(uno, dos, tres) << endl;
    35.  
    36.  return 0;
    37. }
    38.  
    39. //myHeader definition
    40.  void myHeader()
    41. {
    42.  cout << "e-money" <<endl
    43.    << "October 14, 2004" << endl;
    44. }
    45.  
    46. //Now - you need to define the code for this function
    47. double larGest(uno, dos, tres)
    48. {
    49. // Put your code here.
    50. }
    Perion
     
  4. nanobyte

    Thread Starter Senior Member

    May 26, 2004
    118
    1
    Thanks for the info. I have the program running nicely now. I am now trying to add a little more code to the program. After showing the largest of three numbers I want it to take that number and set it to a power that will be the smallest number. Then after that I want the program to take that result and set it to the power of the second largest number. Follow me? Could y'all help me with figuring out how to go about doing this. :) An example like what Perion gave me before would be greatly appreciated. ;)
     
  5. nanobyte

    Thread Starter Senior Member

    May 26, 2004
    118
    1
    Hey, I'm waiting on somebody to help me out here. The attachment below is a copy of the program that I want to add to. The first bit of code is the header file and the code that is about five lines below it is the actual program file. Could somebody help me out, I'm still new to programming. :unsure:
     
  6. alzaro

    New Member

    Oct 26, 2004
    1
    0
    hi I went through your code briefly man, I think the problem is at where you declared your double function"double larGest(double, double, double);"
    UH... I belive you have to name the your double variables in your parameters so lets say you decided to name them n1 through n3 so your function definition should become "double largGest(double n1, double n2, double n3);", and then your function call should be "double largGest(n1, n2, n3)"

    see the problem is that you were using the variable type "double" as your paramters name and that doesn't work in C++

    I hope I am right
     
  7. nanobyte

    Thread Starter Senior Member

    May 26, 2004
    118
    1
    Thanks for the response alzaro, but that wasn't the info I was looking for. I don't have any problems with my program anymore alzaro. The last attachment I gave is a the program that is working. My problem is that I am now trying to add a little more code to the program. After showing the largest of three numbers I want it to take that number and set it to a power that will be the smallest number. Then after that I want the program to take that result and set it to the power of the second largest number. :( Please somebody help me with this; it's driving me crazy! Check out the attachment below!
     
  8. Perion

    Active Member

    Oct 12, 2004
    43
    1
    There's a zillion ways to do this. Here's one.

    Instead of a function - double larGest(double,double,double) - change it to a function that takes the three doubles, sorts them into descending order, and assigns them, largest to smallest, to successive elements of a global array of doubles - maybe the array is called g_dArray. Then you can just use the first element g_dArray[0], which contains the largest double, like you did before. Then use the pow function to raise g_dArray[0] to the power of g_dArray[2] - i.e. the largest to the power of the smallest. I assume you have the pow function. It's like the following and declared in math.h so you need to include the math.h header file:

    double pow(double x, double y); //Raises x to the power of y and returns the result.

    So GLOBALLY declare an array of three doubles.

    Code ( (Unknown Language)):
    1. // A global array of three doubles
    2. double g_dArray[3];
    Then change your larGest function to a function that sorts three doubles in descending order and places them in the global array's sequential elements.

    Code ( (Unknown Language)):
    1. void OrderArray(double n1, double n2, double n3)
    2. {
    3.   // Write code to sort n1, n2, and n3, assigning the largest to g_dArray[0],
    4.   // the next largest to g_dArray[1], and the smallest to g_dArray[2]. Of
    5.   // course, equal numbers are easy to deal with.
    6.   //
    7.   // I ain't gonna write the code - you'll enjoy coming up with some
    8.   // good, efficient, sorting code. Use the array for temporary storage.
    9.   // When complete, g_dArray[0] >= g_dArray[1] >= g_dArray[2]
    10.   }
    Now you can cout the largest of the three like before using g_dArray[0].

    Next, just send the pow function whatever numbers you want raised to what powers and store the results in some double variables that you can use for whatever you had in mind:

    Code ( (Unknown Language)):
    1. // After calling OrderArray(n1,n2,n3) declare some variables for
    2. // storage of the results of calls to the pow function
    3. double dResult1, dResult2;
    4.  
    5. // Here's the largest number raised to the power of the smallest number
    6. dResult1 = pow(g_dArray[0], g_dArray[2]);
    7.  
    8. // Now take that number and raise it to the power of the middle number.
    9. dResult2 = pow(dResult1, g_dArray[1]);
    10.  
    11. // Do whatever with the dResult1 and dResult2 variables
    12.  
    13.  
    (Check out the pow function in your c++ reference. Watch out for overflow and underflow conditions and also make sure you send pow values that it can handle.)

    Perion
     
  9. nanobyte

    Thread Starter Senior Member

    May 26, 2004
    118
    1
    Well I have diligently worked on this program for some time and now have two parts of it kinda working. As you may recall I am trying to make the program:
    1. Prompt the user to enter 3 numbers and show the largest of the three.
    2. Take the largest number and raise to the smallest number.
    3. Take that result and raise it to the middle number.

    I have step1 working, but step2 isn't doing what it's supposed to do. Instead of taking the largest number and raising it to the smallest number, it is taking the last number entered and raising it to the first number entered no matter if it's the largest, smallest, or the middle number or what order the numbers are in.

    The attachment below is the program. The first bit of code is the program file and the code about 8 to 10 lines below it is the header file. Can someone please take a look and tell me what the heck is going. :huh:
     
  10. nanobyte

    Thread Starter Senior Member

    May 26, 2004
    118
    1
    :( Can someone please help me with the program I am trying to create in the above post. Perion gave me some suggestions with arrays, but I am trying to stick with using functions.
     
  11. Perion

    Active Member

    Oct 12, 2004
    43
    1
    Using a global array of doubles to hold the results of sorting was just one possibility. I'd definitly define a sort function (instead of your larGest function) that takes uno, dos, and tres as parameters and sorts them into decending order. If you use pointers to doubles or better yet, reference parameters, in the sort function parameter list it will then modify the variables passed to it.

    Here's an example using reference parameters:

    Code ( (Unknown Language)):
    1. double uno;
    2. double dos;
    3. double tres;
    4.  
    5. cout << "Put three numbers in before I buss your head in." <<endl;
    6. cin >> uno >> dos >> tres;
    7.  
    8. void sort(double& d_one, double& d_two, double& d_three);  //function prototype
    9.  
    10. sort(uno, dos, tres); // Call the sort function. When it returns, uno
    11.                // will contain the largest, dos the middle,
    12.                // and tres the smallest numbers.
    13.  
    14. cout << "\nThe largest of the the three numbers is "
    15.     << uno << endl;
    16.  
    17. // Then you can use uno, dos, and tres in your pow() function to do whatever...
    18.  
    19. // Write a gereric sort function that takes three doubles and puts them
    20. // in order where d_one will be >= d_two >= d_three. When you pass
    21. // uno, dos, and tres to this function they are the variables that actually
    22. // will get modified.
    23.  
    24. void sort(double& d_one, double& d_two, double& d_three)
    25. {
    26.  // Create three temporary double variables.
    27.  // Assign d_one to the first, d_two to the second, etc.
    28.  // After sorting the thre temp vars just assign the largest
    29.  // back to d_one, the middle to d_two, and the smallest to d_three.
    30. }
    Perion
     
  12. nanobyte

    Thread Starter Senior Member

    May 26, 2004
    118
    1
    Okay Perion. I think I'm really close to getting this program finally working. I have two problems left:
    1. How do I go about raising the result of the largest number being raised to the smallest number (the last function -powerhouse2) to the middle number.

    2. (In the first function - powerhouse1)When the numbers entered are all equal I want the c-out statement for first function to be "I'll give you" instead of the other c-out statement which is "The result of the largest number raised to the smallest number is", because I have the program set where when all the numbers are equal that powerhouse1 will c-out "1".

    As before, the first bit of code is the program and the code about 10-11 lines below that is the header file. I'd appreciate anyone help or input. Take a look at the attachment below. :(
     
  13. nanobyte

    Thread Starter Senior Member

    May 26, 2004
    118
    1
    Alright. I've worked on this program diligently again over the weekend and now have everything working the way I want except for that last function at the bottom(powerhouse2) which is supposed to take the result of the powerhouse1,the second function, and raise it to the middle number. My code is in the attachment below. As before, the first bit of code is the program file and the code about 10-11 lines below that is the header file. I'd appreciate anyone's help or input. Could somebody please help me with this last bit of code. :unsure: :(
     
  14. nanobyte

    Thread Starter Senior Member

    May 26, 2004
    118
    1
    Can somebody please help me out with this program!
     
  15. rauhka

    New Member

    Dec 8, 2004
    1
    0
    Hey - I'll give it a shot! Right off the bat however, I would really love you to declare all your function prototypes outside of the MyHeader function, and in full. Its much neater, and much better in practice. But I'm sure you don't want to be lectured about style here! :)

    eg
    Code ( (Unknown Language)):
    1.  
    2.  
    3. //prototypes here!
    4. double larGest(double these, double those, double them);
    5.  
    6. void myHeader()     //function definition
    7. {
    8. ... // not in here!
    9. }
    10.  
    Ok with that out of the way... personally, I would approached this differently, but I will run with what you've got so far.

    From powerhouse1 you know your highest, lowest and middle numbers. Where you assign highrise a value I would call powerhouse2 from there. That way you already know tjhe order of your values.

    eg.
    Code ( (Unknown Language)):
    1.  
    2. if (a > b && b < c && c > a || a==c && a > b)
    3. {
    4.   highrise = pow(c,b);
    5.   cout >> highrise;
    6.   final = powerhouse2(highrise, a); // you know a is the middle value here
    7.   cout >> final;
    8.  
    9. }
    10.   else
    11.  
    12. .... etc
    13.  
    and then a small rewrite of powerhouse2

    Code ( (Unknown Language)):
    1.  
    2. //Raises the result of powerhouse1 to the middle number
    3. double powerhouse2(double a, double power)
    4. {
    5.   double final = pow(a, power);
    6.   return final;
    7. }
    8.  
    You just need to fill in the approprate parts of powerhouse1...

    Does that help?
     
  16. nanobyte

    Thread Starter Senior Member

    May 26, 2004
    118
    1
    I have only one error left now.
     
  17. Perion

    Active Member

    Oct 12, 2004
    43
    1
    Like I said before - I highly recommend sorting the three entered numbers into three variables. Then use the pow(a,b ) function on them however you like.

    Sorting three items into decending order is done by the following algorithm where <> contains the final sorted variable list. There's better ways to sort more than three items but when just three are to be sorted you're pretty much limited to decending the "decision tree" - even using clever swaps doesn't help in this case as far as I know.

    Code ( (Unknown Language)):
    1. if a1 < a2 {
    2.   if a1 < a3 {
    3.     if a2 < a3 {
    4.       // sorted order is <a3, a2, a1>
    5.     } else {
    6.       // sorted order is <a2, a3, a1>
    7.     }
    8.   } else {
    9.     // sorted order is <a2, a1, a3>
    10.   }
    11. } else {
    12.   if a2 < a3 {
    13.     if a1 < a3 {
    14.       // sorted order is <a3, a1, a2>
    15.     } else {
    16.       // sorted order is <a1, a3, a2>
    17.     }
    18.   } else {
    19.     // sorted order is <a1, a2, a3>
    20.   }
    21. }
    So, some code would look like:
    Code ( (Unknown Language)):
    1.  
    2. //
    3. //
    4. // Declare in header file
    5. void sort_three(double a1, double a2, double a3);
    6.  
    7. // Globals
    8. double largest, middle, smallest;
    9.  
    10.  
    11. // somewhere, after getting the input call the sort function
    12. sort_three(uno, dos, tres);
    13.  
    14. // Then use largest, middle, and smallest in pow(a,b) and its results however you wish.
    15.  
    16.  
    17. // Here's the three item sort as a c function:
    18. void sort_three(double a1, double a2, double a3){
    19.   if a1 < a2 {
    20.     if a1 < a3 {
    21.       if a2 < a3 {
    22.         // sorted order is <a3, a2, a1>
    23.         largest  = a3;
    24.         middle   = a2;
    25.         smallest = a1;
    26.       }
    27.       else {
    28.         // sorted order is <a2, a3, a1>
    29.         largest  = a2;
    30.         middle   = a3;  
    31.         smallest = a1;
    32.       }
    33.     }
    34.     else {
    35.       // sorted order is <a2, a1, a3>
    36.       largest  = a2;
    37.       middle   = a1;
    38.       smallest = a3;
    39.     }
    40.   }
    41.   else {
    42.     if a2 < a3 {
    43.       if a1 < a3 {
    44.         // sorted order is <a3, a1, a2>
    45.         largest  = a3;
    46.         middle   = a1;
    47.         smallest = a2;
    48.       }
    49.       else {
    50.         // sorted order is <a1, a3, a2>
    51.         largest  = a1;
    52.         middle   = a3;
    53.         smallest = a2;
    54.       }
    55.     }
    56.     else {
    57.        // sorted order is <a1, a2, a3>
    58.        largest  = a1;
    59.        middle   = a2;
    60.        smallest = a3;
    61.     }
    62.   }
    63. }
    64.  
    Notice that any equal pair is automatically accounted for and just gets left as is. There is no prefered order between equal numbers.

    I know this isn't what you wanted but how can you possibly arrive at a satisfactory program where you need to know the order of three items if you don't bite the bullet and sort the items???

    BTW - if you can find a better sort algorithm (for three items) out there use it! And let me know what it is too...

    Perion
     
  18. nanobyte

    Thread Starter Senior Member

    May 26, 2004
    118
    1
    Ha Ha! I finally got the program working! But I want to add one more function to it called shutDown that will asks the user if he/she wants to run the program again or stop it. Depending on his/her response ('y' or 'Y' for yes and 'n' or 'N' for no) the program will run again or stop. I think you have to use a do/while statement for this but I'm not sure how to approach it. Could somebody show me how to go about doing this function?
     
  19. nanobyte

    Thread Starter Senior Member

    May 26, 2004
    118
    1
    Never mind; I think I got it now. :ph34r: