Page 1 of 2

lulz

Posted: Wed Nov 24, 2010 2:01 pm
by Arce
My teacher once counted off for nesting ternary statement. Well, I found myself waking up 15 minutes before an extra credit assignment was due. I decided, you know what? I can do this in 15, but not without an excess of my friend, Mr. Ternary! Please, direct your attention to the "showResults" mess, and tell me if you think she's going to flunk me? :lol:

Code: Select all

/**
/**
Marcel Girgis
11/24/10
1:57pm

PROGRAM ASSIGNMENT 3

Problem:
   Write a program to
     - Store test grades for all students in a CS class
     - Store data in a 2d array
     - 50 rows by 5 columns
Input Formatting:
    <# telling how many students>
    <grade1><space><grade2><space><grade3><space><grade4><space><grade 5>
Output:
    Via console
Additional Notes:
     - Adjust constants and recompile to change settings
**/

#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>

const unsigned short int MAX_STUDENT_SIZE=50, PADDING_WIDTH=8;

using namespace std;

void   InitializeArray (int sudentRecords[][5]);
int    ReadStudentData (string file_name, int sudentRecords[][5]);
void   ShowStudentRecords(int sudentRecords [][5], int num);
void   ShowResults(int sudentRecords [][5], int num);

int main() {
    int sudentRecords[MAX_STUDENT_SIZE][5];
    int student_counter=0;

    InitializeArray(sudentRecords);

    //Prompt for file names
    string file_name;
    cout << "Enter the file name you would like to read data from: ";
    cin>>file_name;

    //read student data and fetch # read
    student_counter = ReadStudentData(file_name, sudentRecords);
    if(student_counter==-1) return -1; //signifies no students to be read, so exit.


    //echo records
    ShowStudentRecords(sudentRecords, student_counter);


    //do all requested processing and output
    cout << "\nProcessing data...\n"<<endl;
    ShowResults(sudentRecords, student_counter);

    cout << "\nProgram terminating. \n\nSo long, and thanks for the fish! =)"
         <<endl;
    system("pause");
}
/*
Simply initializes our all student data to default values.
This function is part of the program requirements. 

Pre:Takes a 2d array of size MAX_STUDENT_SIZE x 5
Post:  Array values have been set to zero
*/
void InitializeArray(int studetRecords[][5]) {
     for(int i=0;i<MAX_STUDENT_SIZE;i++) { 
          for(int j=0;j<5;j++) {
               studetRecords[i][j]=0;
          }

     }
}

/*
This function uses fstream to read from a file with argument 1's name
and populate our student's data based on the results.
      
pre: constant MAX_STUDENT_SIZE has been properly set
post: array student records now contain all student records.
*/
int ReadStudentData(string file_name, int studentRecords[][5]) {
    fstream file_in;
    int student_counter=0;
    //double total_salary=0;

    file_in.open(file_name.data());
    if(!file_in) {
         cout << "There was an error opening your file!"<<endl;
         return -1; //to signify an error
    }
    cout << "Successfully opened file."<<endl;

    //parse file
    while(!file_in.eof()) {
         //read amount first
         file_in>>student_counter;                 
         
         for(int i=0;i<student_counter&&i<MAX_STUDENT_SIZE;i++) {
              for(int j=0;j<5;j++) {
                   file_in>>studentRecords[i][j];
                   file_in.ignore(); 
                   //file_in.put('\n');
                   if(!file_in)return -1; //there aren't as many available 
                                          //grades as there should be.
              }
         }  
         //ignore the extra newline read after the ID
         file_in.ignore();
    }
    cout << "\nSuccessfully read " << student_counter <<
         " students."<<endl;
    file_in.close();

    return student_counter;
}

//pre: sudent records row size > num
//post: displays all requested results
void ShowResults(int sudentRecords [][5], int num) {
       cout  << setw(PADDING_WIDTH)<<"STUDENT #"
             << setw(PADDING_WIDTH)<<"Average"
             << setw(PADDING_WIDTH)<<"Grade\n";

      double average;
      int averageCounter[5]={0,0,0,0,0}; //index 1 corresponds to A, 2 corresponds to B, etc;
      double highestAverage=0;
      int highestAverageIndex=-1;
      
      for(int i=0;i<num;i++) {
           average=0;
           cout << endl;
           cout << setw(PADDING_WIDTH)<< i;
           
           //count each grade for the current student (j)
           for(int j=0;j<5;j++) {
                average+=sudentRecords[i][j];
           }
           average/=5; //5 grades
           
           //check if we have a new high average
           if(average > highestAverage) {
               highestAverage=average;
               highestAverageIndex=i;
           }
           
           
           cout << setw(PADDING_WIDTH)<<fixed << setprecision(1)<< average;

           //determine where they fall in average
           char letter =  ((average >= 90 && average<= 100 ) ? 'A' :
                          ( average >= 80 && average< 90   ) ? 'B' :
                          ( average >= 70 && average< 80   ) ? 'C' :
                          ( average >= 60 && average< 70   ) ? 'D' :
                          ( average <  60                  ) ? 'F' : 'X') ;
                          
          cout << setw(PADDING_WIDTH)<< letter;  
         
          //increment out counter
          averageCounter[letter-(letter !='F' ? 'A' : 'B' )]++; 
           
      }
      //display averages
      for(int i =0;i<5;i++) 
           cout << endl << "Number of " << char( (i==4? 5 : i)+'A')<<"'s: " << averageCounter[i];
       
      cout << endl << "Highest Average: Student # : " << highestAverageIndex << ", " << highestAverage;
      
          
}
//pre: array studentRecords contains grades for rows j to num
//post: displays to console the contents of student records
void ShowStudentRecords(int sudentRecords [][5], int num) {
      cout   << setw(PADDING_WIDTH)<<"STUDENT #"
             << setw(PADDING_WIDTH)<<"TEST1"
             << setw(PADDING_WIDTH)<<"TEST2"
             << setw(PADDING_WIDTH)<<"TEST3"
             << setw(PADDING_WIDTH)<<"TEST4"
             << setw(PADDING_WIDTH)<<"TEST5\n";
      for(int i=0;i<num;i++) {
           cout << endl;
           cout << setw(PADDING_WIDTH)<< i;
           for(int j=0;j<5;j++) {
               // cout  << setw(PADDING_WIDTH)<< i;
                cout  << setw(PADDING_WIDTH)<<sudentRecords[i][j];
           }
      }
} 


Re: lulz

Posted: Wed Nov 24, 2010 2:14 pm
by adikid89
The ternary operator RULLZ! :worship: You should get a +1 for every ternary you use!

Re: lulz

Posted: Wed Nov 24, 2010 2:15 pm
by Falco Girgis
Written like a true C programmer! Now work on your OBB v terrain culling algorithm, fuckmite.

Re: lulz

Posted: Wed Nov 24, 2010 2:32 pm
by dandymcgee
Did that really save you any time? :roll:
adikid89 wrote:The ternary operator RULLZ! :worship: You should get a +1 for every ternary you use!
Happy 21st birthday!

Re: lulz

Posted: Wed Nov 24, 2010 2:49 pm
by avansc
Have not tested this completely, but should work and save you and a few of those.

Code: Select all

char grade(char val)
{
	return val < 60 ? 'F' : 74-(val/10);
}

Re: lulz

Posted: Wed Nov 24, 2010 3:09 pm
by Arce
Did that really save you any time? :roll:
Of course.

When I said I only had 15 minutes, I meant it. I literally sent my email to her at 1:57 when it was due at 2. I sat at the computer at 1:36.

The reason for all the (unnecessary?) ternary statements is that it allows you to essentially ignore the "design" part and just speed code, "hacking" in extra conditions wherever necessary.

Re: lulz

Posted: Wed Nov 24, 2010 3:14 pm
by Arce
avansc wrote:Have not tested this completely, but should work and save you and a few of those.

Code: Select all

char grade(char val)
{
	return val < 60 ? 'F' : 74-(val/10);
}
Very clever casting, actually! I like, I like. Teacher would probably have shat bricks. XD

But the main thing I was referring to was this garbage:

Code: Select all

          averageCounter[letter-(letter !='F' ? 'A' : 'B' )]++; 

Code: Select all

      for(int i =0;i<5;i++)
           cout << endl << "Number of " << char( (i==4? 5 : i)+'A')<<"'s: " << averageCounter[i];
It's the same as "my logic isn't quite right, but can be fixed immediately with a few ternaries."

Re: lulz

Posted: Wed Nov 24, 2010 5:34 pm
by wearymemory
Arce wrote:Please, direct your attention to the "showResults" mess, and tell me if you think she's going to flunk me? :lol:
Getting flunked for an extra credit assignment—now that I would like to see!
I would mark you down as well, IMHO; pretty nifty, nonetheless. ;)

Re: lulz

Posted: Wed Nov 24, 2010 6:01 pm
by cypher1554R
So, basically.. if your average score is higher than 100, you get

Image

as "x-treeme nerd!" :)

Re: lulz

Posted: Wed Nov 24, 2010 6:02 pm
by Falco Girgis
wearymemory wrote:
Arce wrote:Please, direct your attention to the "showResults" mess, and tell me if you think she's going to flunk me? :lol:
Getting flunked for an extra credit assignment—now that I would like to see!
I would mark you down as well, IMHO; pretty nifty, nonetheless. ;)
Hell no! I have seen some very legit, low-level C code using that kind of embedded ternary statement. I think that's perfectly acceptable.

Re: lulz

Posted: Wed Nov 24, 2010 7:50 pm
by wearymemory
GyroVorbis wrote:
wearymemory wrote:
Arce wrote:Please, direct your attention to the "showResults" mess, and tell me if you think she's going to flunk me? :lol:
Getting flunked for an extra credit assignment—now that I would like to see!
I would mark you down as well, IMHO; pretty nifty, nonetheless. ;)
Hell no! I have seen some very legit, low-level C code using that kind of embedded ternary statement. I think that's perfectly acceptable.
Doh! My apologies, I was not aware that Arce's low-level hacks were necessary for the limited embedded system which he is obviously required to write code for. :lol: I was also unaware of the fact that Arce was required to write in straight C, but I suppose the references to the standard C++ library confused me. :oops: I am also a naive fool for assuming that including an abundance of the code that Arce's professor is specifically against, is clearly insulting their judgment. It is apparent that Arce's professor is dimwitted for enforcing that her students produce clean, readable and maintainable code. Especially in a seemingly basic programming course, where the prerequisites would surely be a knowledge of the bare essentials of the language. Of course, I do not know the situation that Arce is in, so this is merely speculation...seems stressful, though. :roll: Feel free to correct me, if I am wrong.

Re: lulz

Posted: Thu Nov 25, 2010 1:34 am
by Falco Girgis
Wow, my post might have warranted a sarcastic one-liner, but an entire paragraph complete with multiple emoticons to emphasize the sarcasm? When the code in question is ONLY in question because of "personal taste" or "coding style," not efficiency or correctness?

Calm down, sir.

Re: lulz

Posted: Thu Nov 25, 2010 4:23 am
by K-Bal
There is more about code than efficiency and correctness. How about readability, maintainability, modularity? I think the examples in this thread are quite understandable, but I wanted to point that out.

Re: lulz

Posted: Thu Nov 25, 2010 10:52 am
by GroundUpEngine
Brilliant! 8-)

Re: lulz

Posted: Thu Nov 25, 2010 6:01 pm
by Arce
K-Bal wrote:There is more about code than efficiency and correctness. How about readability, maintainability, modularity? I think the examples in this thread are quite understandable, but I wanted to point that out.
Allow me to reiterate:
The reason for all the (unnecessary?) ternary statements is that it allows you to essentially ignore the "design" part and just speed code, "hacking" in extra conditions wherever necessary.
and
But the main thing I was referring to was this garbage:
as well as
Did that really save you any time? :roll:

Of course.
Also,
Hell no! I have seen some very legit, low-level C code using that kind of embedded ternary statement. I think that's perfectly acceptable.
'

was referring to

Code: Select all

           char letter =  ((average >= 90 && average<= 100 ) ? 'A' :
                          ( average >= 80 && average< 90   ) ? 'B' :
                          ( average >= 70 && average< 80   ) ? 'C' :
                          ( average >= 60 && average< 70   ) ? 'D' :
                          ( average <  60                  ) ? 'F' : 'X') ;
hence the
kind of embedded ternary statement.
In regard to
I was also unaware of the fact that Arce was required to write in straight C
Irrelevant? No, non-nonsensical and obviously the retort of somebody who misunderstood the original defense, or atleast drew wild deductions about the whole
I have seen some very legit, low-level C code

to imply anybody implied anything about nested ternaries requiring straight C?
I am also a naive fool
Clearly.
for assuming that including an abundance of the code that Arce's professor is specifically against, is clearly insulting their judgment.
But not for this reason. Perhaps you missed the "lighthearted vibe" of the topic, emphasized by
as "x-treeme nerd!" :)
,
pretty nifty, nonetheless. ;)
,
The ternary operator RULLZ! :worship: You should get a +1 for every ternary you use!
,
Please, direct your attention to the "showResults" mess, and tell me if you think she's going to flunk me? :lol:
pointed out by
Wow, my post might have warranted a sarcastic one-liner, but an entire paragraph complete with multiple emoticons to emphasize the sarcasm?
,
and under the circumstances of
I found myself waking up 15 minutes before an extra credit assignment was due
yet somehow managed to deduce that I was in some way intentionally trying to insult my instructor?

Impressively, you manage to contradict your own logic here
It is apparent that Arce's professor is dimwitted for enforcing that her students produce clean, readable and maintainable code.
with
Especially in a seemingly basic programming course, where the prerequisites would surely be a knowledge of the bare essentials of the language.
; would it not be illogical to argue the simplicity of a "bare essential" course, then swing your own definitions of
readable and maintainable code.
, which is, by and large, a complex topic beyond the scope of
knowledge of the bare essentials of the language
especially when, "showing off" the many uses of a ternary would be clearly demonstrating your
knowledge of the bare essentials of the language
? :roll:

And, finally,
There is more about code than efficiency and correctness. How about readability, maintainability, modularity? I think the examples in this thread are quite understandable, but I wanted to point that out.
this is not necessarily true in the circumstances of some very legit, low-level C code or cases of embedded systems (thought, obviously, this is not applicable here, and we're just arguing for the sake of argument. ;p )
Feel free to correct me, if I am wrong.
Done.