home *** CD-ROM | disk | FTP | other *** search
/ ittybittycomputers.com / www.ittybittycomputers.com.tar / www.ittybittycomputers.com / Courses / Prior / ADS / Sep16.cc < prev    next >
C/C++ Source or Header  |  2006-10-18  |  2KB  |  60 lines

  1. /* Sept.16 Example Solution (C++) */            /*points*/
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. struct arap { int i35[35]; };
  7.  
  8. /*d*/ void oddneg(int & i);                        /*1*/
  9.       void oddneg(int & i) {if (i%2 !=0) i = -i;}  /*2*/
  10.  
  11. int main () {
  12.       int i;
  13. /*a*/ arap loc;                                    /*1*/
  14. /*b*/ arap * dyn = new arap;                       /*1*/
  15.  
  16. /*a*/ for (i=35; i>0; i--) loc.i35[35-i] = i*i;    /*2*/
  17.  
  18. /*c*/ *dyn = loc;                                  /*2*/
  19.  
  20. /*e*/ for (i=0; i<35; i+=3) oddneg(loc.i35[i]);    /*2*/
  21. /*f*/ for (i=0; i<35; i+=5) oddneg(dyn->i35[i]);   /*2*/
  22.  
  23. /*g*/ for (i=0; i<35; i++)                         /*1*/
  24.         cout << i << " " << loc.i35[i] << " " << dyn->i35[i];
  25.  
  26. /*h*/ delete dyn;                                  /*1*/
  27.  
  28. } /* end main */
  29.  
  30. --------------------------------------------------------------
  31.  
  32. /* Sept.16 Example Solution (pure C) */
  33.  
  34. #include <stdio.h>
  35.  
  36. struct araps { int i35[35]; };
  37. typedef struct araps arap;
  38.  
  39. /*d*/ void oddneg(int * i);                        /*1*/
  40.       void oddneg(int * i) {if (*i%2) *i = -(*i);} /*2*/
  41.  
  42. int main () {
  43.       int i;
  44. /*a*/ arap loc;                                    /*1*/
  45. /*b*/ arap * dyn = (arap *) malloc(sizeof(arap));  /*1*/
  46.  
  47. /*a*/ for (i=35; i>0; i--) loc.i35[35-i] = i*i;    /*2*/
  48.  
  49. /*c*/ *dyn = loc;                                  /*2*/
  50.  
  51. /*e*/ for (i=0; i<35; i+=3) oddneg(&loc.i35[i]);   /*2*/
  52. /*f*/ for (i=0; i<35; i+=5) oddneg(&dyn->i35[i]);  /*2*/
  53.  
  54. /*g*/ for (i=0; i<35; i++)                         /*1*/
  55.         printf("%d  %d  %d \n", i, loc.i35[i], dyn->i35[i]);
  56.  
  57. /*h*/ free(dyn);                                   /*1*/
  58.  
  59. } /* end main */
  60.