Bit of programming help

Old and generally outdated discussions, with the rare hidden gem. Enter at your own risk.

Moderators: Haplo, Lead Developers

Locked
User avatar
Stumpytheguar
Member
Posts: 631
Joined: Sun Jun 27, 2004 2:22 am
Location: Irrelevant

Bit of programming help

Post by Stumpytheguar »

Allright so I'm working on a small project for one of my basic C++ classes, and I've run into a small problem. I don't know how to acheive the output specified by the paper while still following the program flow that it describes.

I've got three functions involved in this problem. There's (of course) main, which holds three values - the number of a month, the amount of days in the month, and an array of temperatures for each day.

I've then got Find_high and Find_low, which as the program specifications tell me are supposed to recieve ONLY the array and the number of days in the month, use a quick sorting algorith to find the highest and lowest values, then run through the array again and find all of the dates on which they've occurred, and then output these values to the screen in the date format day/mont (like today is 4/7).

Thus the problem arises - how am I supposed to print the month's number when Find_High and Find_Low don't have acess to the month value? This program is due in 4 hours and I'm clueless as to what I should do.

EDIT: I figured I could pre-print the month value before I called Find_High and Find_Low, but then I'd already need to know how many times the high and low occured! I'm not allowed to create any more functions than the professor requested on the specs sheet, and according to her instructions I can only call each function once (she's very anal about us following her directions).
Retired - Sept. 15, 2005
Stalker
Developer Emeritus
Posts: 4576
Joined: Fri Apr 09, 2004 9:12 pm
Location: Ukraine
Contact:

Post by Stalker »

First I need to say that this is very and very stupid. Second, you can just make a global variable and pass the month number to it... That's the best thing I can think of now. If only you had like a few more hours...
[img]http://img430.imageshack.us/img430/3336/oblivionforum5tb.jpg[/img]

Your friendly slavedriver.
User avatar
Stumpytheguar
Member
Posts: 631
Joined: Sun Jun 27, 2004 2:22 am
Location: Irrelevant

Post by Stumpytheguar »

I know! It's frustrating. I'm considering finding her office and going to ask her about it, because there's no logical explanation. Maybe it's her fault, and she typed up the specs wrong.

The only global variables I know how to use in C++ are constant macros, which can never change once the program starts... and therefore will not work for me, as the number of the month is read off of an input file several times during runtime. For right now, I'm going to work on the other parts of the program, I guess my only solution is to go to the professor myself... I just hope I'm not missing something so completely obvious that I'd think of it...
Retired - Sept. 15, 2005
User avatar
Vernon
Developer Emeritus
Posts: 2145
Joined: Wed Sep 29, 2004 11:57 pm
Location: Sydney, Australia

Post by Vernon »

What kind of array is it?
welp
Thane
Posts: 0
Joined: Wed Mar 30, 2005 7:14 pm
Location: Vivec, Telvanni Canton

Post by Thane »

Dude post your current code and I can help you.
"We are the Knights who say-NI!"
"Now bring us a shrubbery"
"A nice one, but...not too expensive..."
User avatar
Stumpytheguar
Member
Posts: 631
Joined: Sun Jun 27, 2004 2:22 am
Location: Irrelevant

Post by Stumpytheguar »

New code posted below
Last edited by Stumpytheguar on Thu Apr 07, 2005 1:10 pm, edited 1 time in total.
Retired - Sept. 15, 2005
User avatar
cougar
Developer
Posts: 87
Joined: Sun Nov 14, 2004 3:04 pm

Post by cougar »

I think, Stalkers suggestion is the simplest way to resolve this problem. This is how I would define a global variable (instead of the definition on line 23). Like this, you should be able to access and change your variable in every function of your program.

Code: Select all

 
18  void counttemps(int, int*);

    int month = 0;
19
20  void main(void) 
User avatar
Stumpytheguar
Member
Posts: 631
Joined: Sun Jun 27, 2004 2:22 am
Location: Irrelevant

Post by Stumpytheguar »

In C++, you'd can't do that like you do in normal C. I'd have to use #DEFINE, and then it's un-changeable throughout all of the programflow. So, that's not gonna work.

I've somewhat-solved the problem already in the code posted above (which is long-since changed to rid myself of stupid logic errors that I always make).
Retired - Sept. 15, 2005
User avatar
cougar
Developer
Posts: 87
Joined: Sun Nov 14, 2004 3:04 pm

Post by cougar »

Well, I've seen your solution. It's simple, too, but you said, it wouldn't match the specifications. I'm quite sure, you actually can define a global variable like this in C++, I've got some sample code with similar definitions. I'd try it, since it doesn't mean to change that much inside your current code.
User avatar
Stumpytheguar
Member
Posts: 631
Joined: Sun Jun 27, 2004 2:22 am
Location: Irrelevant

Post by Stumpytheguar »

Yeah so now I've got an infinite loop right after findhigh is called, and I have no idea why.

Code: Select all

"arrays.c" 216 lines, 4351 characters 
     1  /***********************************************************/
     2  /*  Andrew Studnicky                                       */
     3  /*  D. Penna                                               */
     4  /* Lab 5 - Array practice                                  */
     5  /* This program inputs temperatures from a text file and   */
     6  /* sorts them into arrays, then provides manipulation.     */
     7  /***********************************************************/
     8  #include <stdio.h>
     9  #include <string.h>
    10  #define MONTHS 12
    11  void printid(void);
    12  void findmonth(int*, int*);
    13  void gettemps(int, int*);
    14  void printmonth(int, int, int*);
    15  void findhigh(int, int, int*);
    16  void findlow(int, int, int*);
    17  void findavg(int, int*);
    18  void sortarray(int, int*);
    19  void counttemps(int, int*);
    20
    21  void main(void)
    22  {
    23      int days = 0;
    24      int month = 0;
    25      int temps[31];
    26
    27
    28      printid();
    29      findmonth(&month, &days);
    30      while (month != -99)
    31      {
    32          gettemps(days, temps);
    33          printmonth(days, month, temps);
    34          findhigh(days, month, temps);
    35          findlow(days, month, temps);
    36          findavg(days, temps);
    37          sortarray(days, temps);
    38          counttemps(days, temps);
    39          findmonth(&month, &days);
    40      }
    41
    42      return;
    43  }
    44
    45  void printid(void)
    46  {
    47      printf("Written by Andrew Studnicky\n4/4/05\nCSI147 - 004\n");
    48      printf("Lab 5:  Practice with arrays and files\n\n\n");
    49      printf("\n***************National Weather Service Report***************\n");
    50      return;
    51  }
    52
    53  void findmonth(int*month, int*days)
    54  {
    55      scanf  ("%d", month);
    56      switch (*month)
    57      {
    58      case 2:  (*days = 28);
    59               break;
    60      case 4:
    61      case 6:
    62      case 9:
    63      case 11: (*days = 30);
    64               break;
    65      default: (*days = 31);
    66      }
    67      return;
    68  }
    69
    70  void gettemps(int days, int*temps)
    71  {
    72      int i;
    73      int temp;
    74      for (i=0; i<days; i++)
    75      {
    76          scanf ("%d", &temp);
    77          temps[i] = temp;
    78      }
    79      return;
    80  }
    81
    82  void printmonth(int month, int days, int*temps)
    83  {
    84      int i;
    85      char monthnames[MONTHS][10] = { {"January"},
    86                                      {"February"},
    87                                      {"March"},
    88                                      {"April"},
    89                                      {"May"},
    90                                      {"June"},
    91                                      {"July"},
    92                                      {"August"},
    93                                      {"September"},
    94                                      {"October"},
    95                                      {"November"},
    96                                      {"December"} };
    97      printf ("\nMonth:\t%s", monthnames[month]);
    98      printf ("\n\nTemperatures:\n\t");
    99      for (i=0; i % 7 < 1; i++)
   100          printf ("%d  ", temps[i]);
   101      printf ("\n");
   102      for (i=7; i % 7 < 2; i++)
   103          printf ("%d ", temps[i]);
   104      printf ("\n");
   105      for (i=14; i % 7 < 3; i++)
   106          printf ("%d ", temps[i]);
   107      printf ("\n");
   108      for (i=21; i % 7 < 4; i++)
   109          printf ("%d ", temps[i]);
   110      printf ("\n");
   111      if (month != 2)
   112      {
   113          for (i=28; i < (days - 1); i++)
   114              printf ("%d ", temps[i]);
   115          printf ("\n");
   116      }
   117
   118      return;
   119  }
   120
   121  void findhigh(int days, int month, int*temps)
   122  {
   123      int high = temps[0];
   124      int i=0;
   125
   126      for (i=1; i<days; i++)
   127          if (temps[i] > high)
   128              high = temps[i];
   129      printf ("\nHigh Temperature for the Month: %d\n", high);
   130
   131      for (i=1; i<days; i++)
   132          if (temps[i] == high)
   133              printf ("   %d\\%d\n", month, i);
   134      return;
   135  }
   136
   137  void findlow(int days, int month, int*temps)
   138  {
   139      int low = temps[0];
   140      int i=0;
   141
   142      for (i=1; i<days; i++)
   143          if (temps[i] < low)
   144              low = temps[i];
   145      printf ("\nLow Temperature for the Month: %d\n", low);
   146
   147      for (i=1; i<days; i++)
   148          if (temps[i] == low)
   149              printf ("   %d\\%d\n", month, i);
   150      return;
   151  }
   152
   153  void findavg(int days, int*temps)
   154  {
   155      float avg=0;
   156      int i=0;
   157
   158      for (i=0; i<days; i++)
   159          avg += temps[i];
   160      avg = (avg / days);
   161      printf ("\n\nAverage temperature for the Month:\t%5.2f", avg);
   162
   163      return;
   164  }
   165
   166
   167  void sortarray(int days, int*temps)
   168  {
   169      int last_day=(days-1),
   170          changes=1,
   171          temp,
   172          i;
   173
   174      while (last_day > 0 && changes)
   175      {
   176          changes = 0;
   177          for (i=0; i<last_day; i++)
   178          {
   179              if (temps[i] < temps[i+1])
   180              {
   181                  temp = temps[i];
   182                  temps[i] = temps[i+1];
   183                  temps[i+1] = temp;
   184                  changes = 1;
   185              }
   186           }
   187          last_day--;
   188      }
   189
   190      return;
   191  }
   192
   193  void counttemps(int days, int*temps)
   194  {
   195      int current=temps[0],
   196          count=1,
   197          i=1;
   198
   199      printf ("\n\nSorted Temperature and Frequency Table\nTemperature -- Frequency\n");
   200
   201      while (i < days)
   202      {
   203          if (temps[i] == current)
   204              count++;
   205          else
   206          {
   207              printf ("%d -- %d\n", current, count);
   208              current = temps[i];
   209              count = 1;
   210          }
   211          i++;
   212      }
   213
   214      return;
   215  }
   216
I guess I'm going to be handing this is late as well, considering that my class started 8 minutes ago...
Retired - Sept. 15, 2005
Stalker
Developer Emeritus
Posts: 4576
Joined: Fri Apr 09, 2004 9:12 pm
Location: Ukraine
Contact:

Post by Stalker »

Stumps, using #define you're defining preprocessing directive. It's not a global. TO define a global - define it like any other variable just not in any function body. Like this for example:

Code: Select all


#include <iostream.h>

int a;

void main()
{
    a=10;
    cout<<"Hello world"<<a<<endl;
}
The result will be this:
Hello world10
[img]http://img430.imageshack.us/img430/3336/oblivionforum5tb.jpg[/img]

Your friendly slavedriver.
User avatar
Stumpytheguar
Member
Posts: 631
Joined: Sun Jun 27, 2004 2:22 am
Location: Irrelevant

Post by Stumpytheguar »

No, the result would be a syntactical compiler error because you're trying to use C commands in a C++ editor. They're similar, but not the same.
Retired - Sept. 15, 2005
User avatar
Zalzidrax
Developer
Posts: 898
Joined: Wed Dec 03, 2003 7:26 am
Location: Bothell, WA

Post by Zalzidrax »

I think your compiler just is stupid...

Works for me. I just tested it.
User avatar
Abramul
Member
Posts: 64
Joined: Tue Mar 01, 2005 7:07 pm

Post by Abramul »

I think you are confusing C and C++. I've never worked with C, only C++, and I remember declaring globals in the fashion demonstrated by Clanker..

P.S. I know CodeWarrior uses cin/cout, but Borland uses something different. Is that just a matter of libraries?
Tamriel is an impossible place, built on impossible precepts. It's, frankly, a magic ball of sentient schizophrenia. --MK
User avatar
Stumpytheguar
Member
Posts: 631
Joined: Sun Jun 27, 2004 2:22 am
Location: Irrelevant

Post by Stumpytheguar »

I'm using the vi editor developed some time in the early eighties. It's NOWHERE near as forgiving as many more popular compilers that will fix your errors FOR you. That answer any questions?
Retired - Sept. 15, 2005
User avatar
Vernon
Developer Emeritus
Posts: 2145
Joined: Wed Sep 29, 2004 11:57 pm
Location: Sydney, Australia

Post by Vernon »

So why are using that particular editor? It also worked for me - you like your whitespace, don't you? |-P
welp
Starcrunch
Developer Emeritus
Posts: 1649
Joined: Tue Aug 17, 2004 5:12 am
Location: DC, USA

Post by Starcrunch »

He's probably using that editor because he will submit his code and the TA will compile it using that editor (at least thats how my C and FORTRAN classes worked).

-Starcrunch
Stalker
Developer Emeritus
Posts: 4576
Joined: Fri Apr 09, 2004 9:12 pm
Location: Ukraine
Contact:

Post by Stalker »

Abramul wrote: in the fashion demonstrated by Clanker..
Refering to me ? ;)
No, the result would be a syntactical compiler error because you're trying to use C commands in a C++ editor. They're similar, but not the same.
Actually the only difference between C and C++ is classes. I'm using Microsoft Visual Studio BTW. And it does compile OK.
[img]http://img430.imageshack.us/img430/3336/oblivionforum5tb.jpg[/img]

Your friendly slavedriver.
Locked