Thread: Code writing issues Part 2...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    8

    Code writing issues Part 2...

    Okay, okay...I tried to write the code myself...I figured someone would be cool enough to do me a favor, but I guess there's some moral programming "code" I am unaware of. Anyway, here's what I have. Please judge and modify and add and erase to your hearts content.
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    void doOneSet(char opc)
    {
       char str[100] = { 0 };
       
       switch(opc)
       {
          int i;
          
          case '+': for(i=0; i < 5; i++){
             
                        sprintf(str, "%2d + %2d = ", rand() % 100, rand() % 100);
                        puts(str);
                    }
                    
                    break;
       }
    }  
    
    int main(void)
    {
       srand((unsigned)time(NULL));
       
       doOneSet('+');         
       
       return 0;
    }
    
    
    
    switch(opc){
    
       int i, n1, n2;
    
       case '+': for(i=0; i < 5; i++){
    
                   n1 = rand() % 100; n2 = rand() % 100;
                   sprintf(str, "%2d + %2d = %3d", n1, n2, n1 + n2);
                   puts(str);
                 }
    
                 break;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    So what are you trying to do?

    A switch statement must be in a function.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Linker.exe64
    Join Date
    Mar 2005
    Location
    Croatia,Pozega
    Posts
    37
    Quote Originally Posted by jaybo684
    Okay, okay...I tried to write the code myself...I figured someone would be cool enough to do me a favor, but I guess there's some moral programming "code" I am unaware of.
    Just to help you about this...
    What if I asked you to make me a string parser in c++, and offering you music or something like that? What would you do? Write me whole program?
    Then think about monastery, they need people like you.

    I see that you are new to forum, so lete me give you few advices about posting topics :

    I.
    Why have you made new thread for this? It should go in the old one, where you first posted and where people asked you to show your effort.
    I don't see any reason for opening new thread.

    II.
    When making new thread, think of some meaningful name for it. What does "Code writing issues" tell you ? For me it means that someone has problems/questions about something. When I opened thread, I saw completely different thing.


    Don't be offended becouse of this, I just wanted to teach you some basic principles about forums.


    Nice to see that you started writing code.
    That is the only way people will help you... If they see that you are trying.
    Remember, never ask someone to write you full program. Except if you are offering car/money/wife .


    Greetings,
    Binary_Dragon
    Code:
     
         .J?+.                             ?`+.
       .+1P .++.                         ,+` 4++.
      .+zq\ .:.?i                      .!`?  .yz+.
      ?zdd ..:.^`J.                   ,!..?...Kyz+	
     .+dXM ..!p^.N.+                 ,,^.a..`.#XO+.
     .zwWM ^`.Mc`JMhJ.             .:JF`JM..^.#WOz`
      jwpMr`..NF.JMMM,!           .JMMF.dJ..`JNWrc
       0Wgb `!B:.MMMMM,:         ,.MMMF.j$.` NHSZ`
        TWMp`.+;`MMMMMM.:       ;.MMMMM.;+``JHW=	
          7Mh,``JMMMM"`          .TMMMMb``.H#"`
    

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Quote Originally Posted by 81N4RY_DR460N
    I.
    Why have you made new thread for this? It should go in the old one, where you first posted and where people asked you to show your effort.
    I don't see any reason for opening new thread.

    You must have missed that they locked/closed the last one
    he was force to make a new one.

  5. #5
    Linker.exe64
    Join Date
    Mar 2005
    Location
    Croatia,Pozega
    Posts
    37
    I excuse for my stupidity...
    Haven't seen this locked icon.
    ...thanks for telling me this horny vector
    Code:
     
         .J?+.                             ?`+.
       .+1P .++.                         ,+` 4++.
      .+zq\ .:.?i                      .!`?  .yz+.
      ?zdd ..:.^`J.                   ,!..?...Kyz+	
     .+dXM ..!p^.N.+                 ,,^.a..`.#XO+.
     .zwWM ^`.Mc`JMhJ.             .:JF`JM..^.#WOz`
      jwpMr`..NF.JMMM,!           .JMMF.dJ..`JNWrc
       0Wgb `!B:.MMMMM,:         ,.MMMF.j$.` NHSZ`
        TWMp`.+;`MMMMMM.:       ;.MMMMM.;+``JHW=	
          7Mh,``JMMMM"`          .TMMMMb``.H#"`
    

  6. #6
    Banned
    Join Date
    Jun 2005
    Posts
    594
    np lol,

  7. #7
    Linker.exe64
    Join Date
    Mar 2005
    Location
    Croatia,Pozega
    Posts
    37
    Jaybo684,
    This is too easy...
    I've made it in C++, it's not full, but you can translate it in C, and add what you need.
    The rules are pretty strict... ( http://www.santarosa.edu/~ssarkar/ci...ture%20Diagram )
    You will have much work about this.............

    WARNING, 122 line code block ahead
    Code:
    /*********************************************************************
     [Author]:
         Binary_Dragon
     
     [Date]:
         1.8.2005
    
     ********************************************************************/
    
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    //------------------------------------
    //	PROTOTYPES
    void makeSet(char math_operator);
    bool checkAnswer(double num1, double num2, double usersAnswer);
    
    //------------------------------------
    int main (int argv[], char* argc)
    {
    	time_t *currTime = new time_t();
    
    	cout << currTime << endl;
    	srand ((unsigned int)currTime);
    
    	makeSet('+');
    
    	cin.get();
    	cin.get();
    	return 0;
    }
    
    
    //------------------------------------
    //	holds settings
    struct SETTINGS
    {
    public:
    	SETTINGS()
    	{
    		cout << "\nHow much problems do you want?";
    		cin >> numOfProblems;
    		cout << "\nAnd what will be maximal number ?";
    		cin >> maxNum;
    	}
    
    	int numOfProblems;
    	int maxNum;
    } ;
    
    
    //------------------------------------
    //	Generates random numbers
    double genNums(int maxNumber)
    {
    	return double(rand() % maxNumber);
    }
    
    
    //------------------------------------
    //	Makes set
    void makeSet (char math_operator)
    {
    	static int numOfSets = 1;
    	double br1 = 0;
    	double br2 = 0;
    	double answer;
    
    	SETTINGS pref;
    
    
    	cout << "\n---------------------------------------\nSET "
    		 << numOfSets;
    	
    	switch (math_operator)
    	{
    		case '+' :
    			{
    				cout << "\n====================================\n"
    					<< "\t\tAddition  [ + ]";
    				for (int i=1; i<=pref.numOfProblems; i++)
    				{
    					br1 = genNums(pref.maxNum);
    					br2 = genNums(pref.maxNum);
    
    					cout << "\n*************************************\n"
    						 << "Problem " << i << ", " 
    						 << (pref.numOfProblems - i) << " to go...";
    
    					cout << "\n" << br1 << " + " << br2 << " = ";
    					cin  >> answer;
    					
    					if ( checkAnswer(br1, br2, answer) )
    						cout << "\tCORRECT!";
    					else
    						cout << "\tINCORRECT!!!";
    
    				}
    			}
    		break;
    		
    		default:
    			cout << "\nInvalid operator!!";
    		break;
    	}
    
    numOfSets++;
    }
    
    //------------------------------------
    bool checkAnswer (double num1, double num2, double usersAnswer)
    {
    	if (usersAnswer == num1 + num2)
    		return true;
    	else
    		return false;
    }
    
    //------------------------------------
    Code:
     
         .J?+.                             ?`+.
       .+1P .++.                         ,+` 4++.
      .+zq\ .:.?i                      .!`?  .yz+.
      ?zdd ..:.^`J.                   ,!..?...Kyz+	
     .+dXM ..!p^.N.+                 ,,^.a..`.#XO+.
     .zwWM ^`.Mc`JMhJ.             .:JF`JM..^.#WOz`
      jwpMr`..NF.JMMM,!           .JMMF.dJ..`JNWrc
       0Wgb `!B:.MMMMM,:         ,.MMMF.j$.` NHSZ`
        TWMp`.+;`MMMMMM.:       ;.MMMMM.;+``JHW=	
          7Mh,``JMMMM"`          .TMMMMb``.H#"`
    

  8. #8
    Banned
    Join Date
    Jun 2005
    Posts
    594
    thsi code interested me to so i made my own version,
    very different yet not :

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
    	srand(time(NULL));
    	time_t start, end;
    	double diff;
    	double correct = 0;
    	double all = 0;
    	cout << "Press a Key to Start your 20 Question Exam!" << endl << endl;
    	cin.get();
    	time (&start);
    	ofstream out ("mathlog.txt");
    	while(all != 20)
    	{
    		int total = 0;
    		int rand1 = rand()%100;
    		int rand2 = rand()%100;
    		cout << rand1 << " + " << rand2 << " = ";
    		out << rand1 << " + " << rand2 << " = ";
    		cin >> total;
    		out << total << endl;
    		if((rand1 + rand2) == total)
    		{
    			cout << "Correct!" << endl;
    			out << "Correct!" << endl;
    			correct++;
    		}
    		else
    		{
    			cout << "Incorrect!" << endl;
    			out << "Incorrect!" << endl;
    		}
    		all++;
    	}
    	time (&end);
    	diff = difftime (end,start);
    	cout << "You got " << correct << " correct out of " << all << " for an accuracy rate of " << setprecision(0) << ((correct/all)*100) << endl;
    	out << "You got " << correct << " correct out of " << all << " for an accuracy rate of " << setprecision(0) << ((correct/all)*100) << endl;
    	cout << "This test took you : " << diff << " seconds." << endl;
    	out << "This test took you : " << diff << " seconds." << endl;
    	cout << "Done!" << endl;
    	cin.ignore();
    	cin.get();
    	out.close();
    	return 0;
    }

  9. #9
    Linker.exe64
    Join Date
    Mar 2005
    Location
    Croatia,Pozega
    Posts
    37
    vector, you can find more of those at http://www.santarosa.edu/~ssarkar/cis10sum05/ .Just go to assignments.
    @Jaybo
    What do you have now?
    Code:
     
         .J?+.                             ?`+.
       .+1P .++.                         ,+` 4++.
      .+zq\ .:.?i                      .!`?  .yz+.
      ?zdd ..:.^`J.                   ,!..?...Kyz+	
     .+dXM ..!p^.N.+                 ,,^.a..`.#XO+.
     .zwWM ^`.Mc`JMhJ.             .:JF`JM..^.#WOz`
      jwpMr`..NF.JMMM,!           .JMMF.dJ..`JNWrc
       0Wgb `!B:.MMMMM,:         ,.MMMF.j$.` NHSZ`
        TWMp`.+;`MMMMMM.:       ;.MMMMM.;+``JHW=	
          7Mh,``JMMMM"`          .TMMMMb``.H#"`
    

  10. #10
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Jaybo, are you programming in C or C++?

    Code:
    sprintf(str, "%2d + %2d = %3d", n1, n2, n1 + n2);
    puts(str);

    Second, haven't you missed the deadline for this particular project?

    Third why are you two posting solutions to his homework? Let him try it out?


  11. #11
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> but I guess there's some moral programming "code" I am unaware of.

    Homework policy

    It is posted at the top of the forums, by the board rules. You did read the rules didn't you?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code writing issues..
    By jaybo684 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2005, 07:51 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 01:27 PM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM