lulz

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

lulz

Post 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];
           }
      }
} 

<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: lulz

Post by adikid89 »

The ternary operator RULLZ! :worship: You should get a +1 for every ternary you use!
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: lulz

Post by Falco Girgis »

Written like a true C programmer! Now work on your OBB v terrain culling algorithm, fuckmite.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: lulz

Post 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!
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: lulz

Post 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);
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

Re: lulz

Post 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.
Last edited by Arce on Wed Nov 24, 2010 3:18 pm, edited 1 time in total.
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

Re: lulz

Post 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."
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

Re: lulz

Post 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. ;)
User avatar
cypher1554R
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1124
Joined: Sun Jun 22, 2008 5:06 pm

Re: lulz

Post by cypher1554R »

So, basically.. if your average score is higher than 100, you get

Image

as "x-treeme nerd!" :)
Last edited by cypher1554R on Wed Nov 24, 2010 6:04 pm, edited 1 time in total.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: lulz

Post 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.
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

Re: lulz

Post 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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: lulz

Post 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.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: lulz

Post 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.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: lulz

Post by GroundUpEngine »

Brilliant! 8-)
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

Re: lulz

Post 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.
Last edited by Arce on Thu Nov 25, 2010 6:10 pm, edited 4 times in total.
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
Locked