Author Topic: C programming problem  (Read 4454 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
C programming problem
« on: January 31, 2011, 09:08:38 am »
Code: [Select]
#include <stdio.h>

main() {
    // This program solves the f91 recursive function for a given number
    // This program was coded by David
    printf("Enter number: ");
    int input;
   
    scanf("%d",input);
   
    printf("The result is: ",f91(input));
   
    getch();
    return 0; 
}

int f91(n) {
    if (n<101)
    {
        return f91(f91(n+11));           
    }   
    else
    {
        return n-10;
    }
}

This is my code, which is supposed to solve the f91 function. I was converting it from my other python program here.

The program allows input but then crashes. Any idea?

Offline Galandros

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1140
  • Rating: +42/-10
    • View Profile
Re: C programming problem
« Reply #1 on: January 31, 2011, 09:52:54 am »
Try     scanf("%d",&input);

Note the & as pointer to operator.
Hobbing in calculator projects.

Offline Tribal

  • The Fallen
  • LV5 Advanced (Next: 300)
  • *
  • Posts: 218
  • Rating: +15/-1
    • View Profile
Re: C programming problem
« Reply #2 on: January 31, 2011, 04:31:39 pm »
Also change the printf to:
Code: [Select]
printf("The result is: %i",f91(input));So that it will show the output of the function ;)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: C programming problem
« Reply #3 on: February 02, 2011, 02:18:03 pm »
Thank you two, now who can explain me what %i stands for?
* Scout just googled!

Thanks ;D

Offline Ranman

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1354
  • Rating: +83/-0
    • View Profile
Re: C programming problem
« Reply #4 on: February 02, 2011, 02:44:50 pm »
Thank you two, now who can explain me what %i stands for?
* Scout just googled!

Thanks ;D

The % is a special token. It tells the compiler that the next symbol (in this case 'i') will be used for specialized text formatting. The 'i' is for a 16 bit integer. Hence, the integer (returned from the call to f91(input)) will be inserted into the string to print to the screen.

Take a look at the The C Book: Formatted I/O chapter.
« Last Edit: February 02, 2011, 02:46:14 pm by Ranman »
Ranman
Bringing Randy Glover's Jumpman to the TI-89 calculator. Download available at Ticalc.

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: C programming problem
« Reply #5 on: February 02, 2011, 02:49:33 pm »
Thank you two, now who can explain me what %i stands for?
* Scout just googled!

Thanks ;D

The % is a special token. It tells the compiler that the next symbol (in this case 'i') will be used for specialized text formatting. The 'i' is for a 16 bit integer. Hence, the integer (returned from the call to f91(input)) will be inserted into the string to print to the screen.

Take a look at the The C Book: Formatted I/O chapter.
Actually, I'd think it would be a 32-bit integer unless you're using a really old computer. At any rate, it should be used for "int" values.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Ranman

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1354
  • Rating: +83/-0
    • View Profile
Re: C programming problem
« Reply #6 on: February 02, 2011, 02:56:29 pm »
Actually, I'd think it would be a 32-bit integer unless you're using a really old computer. At any rate, it should be used for "int" values.
Well... You are correct. 'i' stands for the native "word" length of a particular CPU. For our 68K calcs (Motorola 68000 cpu) the "word" length is 16 bits... you would have to use 'l' to properly print a 32 bit integer. For modern CPUs, the word length is 32bits... so 'i' would print a 32bit integer.
« Last Edit: February 02, 2011, 03:05:27 pm by Ranman »
Ranman
Bringing Randy Glover's Jumpman to the TI-89 calculator. Download available at Ticalc.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: C programming problem
« Reply #7 on: February 02, 2011, 06:50:30 pm »
I knew what % did, just not %i, INTEGER.

Thanks anyways ;D