-
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));
}
-
> 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.
-
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]);