Thread: help in transfoming C++ code into regular ansi C..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    help in transfoming C++ code into regular ansi C..

    the instruction for building this program is not allowing me
    to call functions,use iosteam ,use arrays

    what they want is just If ,while,for commands , simple variable,only stdio.h
    in C

    how to transform this code into that??

    Code:
    #include <iostream>
    
    unsigned sum_of_divisors(unsigned n) {
      int result=1;
     
      for(unsigned k=2;k*k<=n;++k) {
        if(n&#37;k==0) {
          int sum_of_powers=1, power=1;
          do {
            power*=k;
            sum_of_powers+=power;
            n/=k;
          } while(n%k==0);
          result*=sum_of_powers;
        }
      }
      return n>1? (n+1)*result : result;
    }
    
    bool is_abundant(unsigned n) {
      // The body of this function can be substituted by simply                     
      // return sum_of_divisors(n)>2*n;                                             
      // This is a memoized version.                                               
       const int cache_size=1000000;
      static char cache[cache_size]={0};
      if(n<cache_size && cache[n])
        return cache[n]-1;
      bool result=(sum_of_divisors(n)>2*n);
      if(n<cache_size)
        cache[n] = 1+result;
      return result;
    }
    
    void print_if_sum_of_abundants(unsigned n) {
      for(unsigned i=1;i*2<=n;++i) {
        if(is_abundant(i)&&is_abundant(n-i)) {
          std::cout << n << " = " << i << " + " << (n-i) << '\n';
          return;
        }
      }
    }
    
    int main() {
      int limit;
      std::cout << "Enter limit: ";
      std::cin >> limit;
     
      for(unsigned i=1;i<=limit;++i)
        print_if_sum_of_abundants(i);
    }
    Last edited by transgalactic2; 12-08-2008 at 02:26 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    so where is your attemp to solve the problem?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i changed the iosteam into stdio.h
    i wrote unsigned k; outside
    and deleted the unsigned in for

    but thats as far as my knowledge in c++ goes

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    replace std::cout << with printf
    std::cin >> with scanf
    and show the resulting code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    still its not running

    ??
    Code:
    #include <stdio.h>
    
    unsigned sum_of_divisors(unsigned n) {
      int result=1;
    unsigned k;
      for(  k=2;k*k<=n;++k) {
        if(n&#37;k==0) {
          int sum_of_powers=1, power=1;
          do {
            power*=k;
            sum_of_powers+=power;
            n/=k;
          } while(n%k==0);
          result*=sum_of_powers;
        }
      }
      return n>1? (n+1)*result : result;
    }
    
    bool is_abundant(unsigned n) {
      // The body of this function can be substituted by simply
      // return sum_of_divisors(n)>2*n;
      // This is a memoized version.
       const int cache_size=1000000;
      static char cache[cache_size]={0};
      if(n<cache_size && cache[n])
        return cache[n]-1;
      bool result=(sum_of_divisors(n)>2*n);
      if(n<cache_size)
        cache[n] = 1+result;
      return result;
    }
    
    void print_if_sum_of_abundants(unsigned n) {
      unsigned i;
      for(i=1;i*2<=n;++i) {
        if(is_abundant(i)&&is_abundant(n-i)) {
            printf("%d=%d+%d\n",n,i,n-i);
    
          return;
        }
      }
    }
    
    int main() {
      int limit;
      printf(" "Enter limit: ");
      scanf("%d",&limit);
    unsigned i;
      for(i=1;i<=limit;++i)
        print_if_sum_of_abundants(i);
    }
    Last edited by transgalactic2; 12-08-2008 at 02:55 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What does not running mean?
    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.

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i get these syntax errors from the compilers:
    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|20|error: syntax error before "is_abundant"|
    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|20|warning: return type defaults to `int'|
    C:\Documents and Settings\lun\My Documents\ass1\ass1.c||In function `is_abundant':|
    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|25|error: variable-sized object may not be initialized|
    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|25|warning: excess elements in array initializer|
    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|25|warning: (near initialization for `cache')|
    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|25|error: storage size of 'cache' isn't constant|
    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|28|error: `bool' undeclared (first use in this function)|
    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|28|error: (Each undeclared identifier is reported only once|
    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|28|error: for each function it appears in.)|
    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|28|error: syntax error before "result"|
    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|30|error: `result' undeclared (first use in this function)|
    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|25|error: size of variable 'cache' is too large|
    C:\Documents and Settings\lun\My Documents\ass1\ass1.c||In function `main':|
    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|47|error: syntax error before "Enter"|
    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|47|error: missing terminating " character|
    ||=== Build finished: 11 errors, 3 warnings ===|

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Firstly, can you use / are you using C99?
    And secondly, this has little to do with C++ anymore - mostly syntax errors, undeclared identifiers and variables not defined at the start of a block.
    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.

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i am using codeblocks8.02 compiler
    i dont know if its using C99

    i got a function is_abundant(unsigned n) defined as bool

    i know thats its not possible in C
    and

    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|25|error: storage size of 'cache' isn't constant|

    ??

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    and how to interpret this line :

    return n>1? (n+1)*result : result;

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by transgalactic2 View Post
    i am using codeblocks8.02 compiler
    i dont know if its using C99
    Code::Blocks is not a compiler; it's an IDE.

    i got a function is_abundant(unsigned n) defined as bool

    i know thats its not possible in C
    It's possible in C99.
    Otherwise just typedef it.

    and

    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|25|error: storage size of 'cache' isn't constant|

    ??
    Make:
    Code:
    	const int cache_size=1000000;
    	static char cache[cache_size]={0};
    Into:
    Code:
    	static char cache[1000000]={0};
    C++ can take constants that can be evaluated at compile time to specify the size of an array, while C cannot.

    Quote Originally Posted by transgalactic2 View Post
    and how to interpret this line :

    return n>1? (n+1)*result : result;
    Code:
    if (n > 1)
        return (n + 1) * result;
    else
        return result;
    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.

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i did the changes you told

    i cant use bool type
    and
    i am not allowed to use typedef on the bool problem

    how to replace it with intege variables of 1 and 0??

    i get a bug that cache_size is not defined in is_abundant(unsigned n)

    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|30|error: `cache_size' undeclared (first use in this function)|

    i dont know how to define cache_size

    i cant use type unsigned only integer/long integer
    how to replace it??
    Code:
    #include <stdio.h>
    
    unsigned sum_of_divisors(unsigned n) {
      int result=1;
    unsigned k;
      for(  k=2;k*k<=n;++k) {
        if(n&#37;k==0) {
          int sum_of_powers=1, power=1;
          do {
            power*=k;
            sum_of_powers+=power;
            n/=k;
          } while(n%k==0);
          result*=sum_of_powers;
        }
      }
      if (n > 1)
        return (n + 1) * result;
    else
        return result;
    }
    
    int is_abundant(unsigned n) {
    
      // The body of this function can be substituted by simply
      // return sum_of_divisors(n)>2*n;
      // This is a memoized version.
      static char cache[1000000]={0};
    
      if(n<cache_size && cache[n])
        return cache[n]-1;
      bool result=(sum_of_divisors(n)>2*n);
      if(n<cache_size)
        cache[n] = 1+result;
      return result;
    }
    
    void print_if_sum_of_abundants(unsigned n) {
      unsigned i;
      for(i=1;i*2<=n;++i) {
        if(is_abundant(i)&&is_abundant(n-i)) {
            printf("%d=%d+%d\n",n,i,n-i);
    
          return;
        }
      }
    }
    
    int main() {
      int limit;
      printf(" "Enter limit: ");
      scanf("%d",&limit);
    unsigned i;
      for(i=1;i<=limit;++i)
        print_if_sum_of_abundants(i);
    }
    Last edited by transgalactic2; 12-08-2008 at 04:06 AM.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by transgalactic2 View Post
    i cant use bool type
    and
    i am not allowed to use typedef on the bool problem
    ...And why not?

    how to replace it with intege variables of 1 and 0??
    You can't. But you can use an integer or whatever that can holds values more than 0 and 1. It works, as well, but not limited to only 1 and 0.

    i get a bug that cache_size is not defined in is_abundant(unsigned n)

    C:\Documents and Settings\lun\My Documents\ass1\ass1.c|30|error: `cache_size' undeclared (first use in this function)|
    Sigh, so make a define!

    i cant use type unsigned only integer/long integer
    how to replace it??
    Unless you use C99, no.
    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.

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i am restricted to these simple commands
    because we havent studied them in the course

    i cant use define


    i can use visual studio 2005
    but only a simple commands

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So hard-code them, then.
    If you haven't studied them in a course, then why are you trying to change C++ code that contains these things into C code?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM