Simulating an Adder

This is a discussion on Simulating an Adder within the C Programming forums, part of the General Programming Boards category; I am having trouble with the "for loop" of a 32 bit Adder program. I am suppose to take 2 ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    13

    Simulating an Adder

    I am having trouble with the "for loop" of a 32 bit Adder program. I am suppose to take 2 arrays of 32 bits and sum them together.

    The 2 errors I get are on line 37 and 38 called "called object is not in function".

    I don't know how to implement the for loop. Can anyone help or point me in the right direction?

    Code:
    #include "stdio.h"
    #include "stdlib.h"
    
    
    int sum(int x[], int y[], int c);
    int carry(int d[], int e[], int f);
    
    int main (void){
        
        int a[31], b[31], sum[31];
        int x,y,c;
        int d,e,f;
        int i;
    
        printf("Enter 32 bits: ");
        scanf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", 
        &a[31], &a[30], &a[29], &a[28], 
        &a[27], &a[26], &a[25], &a[24], &a[23], &a[22], &a[21], &a[20], &a[19], &a[18], &a[17], 
        &a[16], &a[15], &a[14], &a[13], &a[12], &a[11], &a[10], &a[9], &a[8], &a[7], 
        &a[6], &a[5], &a[4], &a[3], &a[2], &a[1], &a[0] );
        
        printf("Enter 32 bits: ");
        scanf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", 
        &b[31], &b[30], &b[29], &b[28], 
        &b[27], &b[26], &b[25], &b[24], &b[23], &b[22], &b[21], &b[20], &b[19], &b[18], &b[17], 
        &b[16], &b[15], &b[14], &b[13], &b[12], &b[11], &b[10], &b[9], &b[8], &b[7], 
        &b[6], &b[5], &b[4], &b[3], &b[2], &b[1], &b[0] );
        
        
          int carryIn=0;   //Initialize 0 to carryIn
          
         for(i=0;i<32;i++){
        
         sum(a[i],b[i],c); //compute the sum
         carry(a[i],b[i],carryIn); // compute the carryover
         
         sum[i] = sum(a[i],b[i],c);
         carryIn = carry(a[i],b[i],carryIn);
                    
                           }
        
        system("PAUSE");
        return 0;
    } 
        int sums(int x[], int y[], int c){
            
            return ((!x && !y && c)||
                   (!x && y && !c) ||
                   (x && !y && !c) ||
                   (x && y && c));
        }
        
        int carry(int d[], int e[], int f){
            
            return ((!d && e && f) || 
                    (d && !e && f) ||
                    (d && e && !f) ||
                    (d && e && f));
        }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > int a[31], b[31], sum[31]
    You need [32] if you're going to store 32 numbers.

    > The 2 errors I get are on line 37 and 38 called "called object is not in function".
    You have a local array called 'sum' and a global function called 'sum'.
    Rename one of them.
    Though it would appear you later call your function 'sums'

    Oh, and neither of your functions should be taking array parameters, according to your usage.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    this was posted earlier and we said you should use scanf to read those integers

    Code:
    int i = 0;
    for (; i < 32; i++) 
        scanf("%d", &a[i]);
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulating keyboard event on Windows xp
    By GOBLIN-85 in forum Windows Programming
    Replies: 5
    Last Post: 08-17-2009, 08:27 AM
  2. Replies: 6
    Last Post: 08-28-2008, 08:10 PM
  3. need help with hex adder.
    By webznz in forum C Programming
    Replies: 5
    Last Post: 03-24-2008, 11:10 PM
  4. Simulating mouse movement in other windows
    By Wiretron in forum Windows Programming
    Replies: 11
    Last Post: 10-13-2007, 08:11 AM
  5. simulating key with c++
    By adr in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2006, 03:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21