Thread: my Quiz

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1

    Question my Quiz

    hi all

    today I had a quiz in template and recursion
    it was easy I did will , but when I tried to solve the questions in VB it gave me different answers I will not say my answers untill I see urs to compare them with mine.
    so help me pleas to find where the errors, is it from me or the compiler?

    Code:
    #include<iostream>
    using namespace std;
    
    
    int sum(int m,int n){
    	if (m==0)
    	return n+1;
    if (n==0)
    return sum (m-1 , 1);
    else
     sum (m-1 , sum ( m , n-1));
    cout<<"done";
    } 
    
    int main(){
    
    
    cout<<sum(2,5);
    
    
    	return 0;}

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh my goodness, dude, learn to indent!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What's it supposed to calculate? 2 + 5? Note that in some cases sum doesn't return anything, so it can't give good results.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Mmm, looks like this - http://en.wikipedia.org/wiki/Ackermann_function
    So (apart from the coding error anon spotted), lets say arithmetic overflow, or you blow the stack.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by hanadi View Post
    hi all

    today I had a quiz in template and recursion
    it was easy I did will , but when I tried to solve the questions in VB it gave me different answers I will not say my answers untill I see urs to compare them with mine.
    so help me pleas to find where the errors, is it from me or the compiler?

    Code:
    #include<iostream>
    using namespace std;
    
    
    int sum(int m,int n){
    	
        if (m==0)
    	return n+1;
        else if (n==0)
            return sum (m-1 , 1);
        else
             sum (m-1 , sum ( m , n-1));
    
    cout<<"done";
    } 
    
    int main(){
    
        cout<<sum(2,5);
    
        return 0;}
    Notice the bolded item that you missed.

    Also, the only output here is "done".
    Last edited by DaveH; 01-14-2009 at 12:37 PM.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    All elses in the function are redundant, since the then branches return early.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quiz on C
    By ganesh bala in forum C Programming
    Replies: 18
    Last Post: 02-10-2009, 03:49 PM
  2. Try this quiz (just made it with C)
    By voltson4 in forum C Programming
    Replies: 1
    Last Post: 07-11-2003, 06:28 PM
  3. C programming language quiz
    By Trancongan in forum C Programming
    Replies: 7
    Last Post: 04-21-2002, 01:04 PM
  4. C programming quiz
    By Denise in forum C Programming
    Replies: 2
    Last Post: 11-09-2001, 04:49 AM
  5. Brainstorm quiz
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 11-08-2001, 12:04 PM