-2

I am trying to write a program where there will be two functions with large blocks of code. Each of the functions will be calling a few smaller functions. Now, in the main program body, I need to call the two larger functions. I am using arrays in the program.

I have done a sample but I am getting errors. Kindly please rectify my code. I am quite a newbie in this field. So please help.

# include <stdio.h>

unsigned char a[4] = {5, 3, 6, 2};
unsigned char b[4] = {3, 5, 2, 5};
unsigned char c[4] = {4, 2, 5, 7};
unsigned char x[4];
unsigned char y[4];

void add(unsigned char* num1, unsigned char* num2, unsigned char* num3)
{
  int k;
  for (k=0; k<3; k++)
  {
   num3[k] = num1[k] + num2[k]);
  }
}

void mul(unsigned char* num1, unsigned char* num2, unsigned char* num3)
{
  int k;
  for (k=0; k<3; k++)
  {
    num3[k] = num1[k] * num2[k]);
  }
}

void test1(unsigned char* num1, unsigned char* num2, unsigned char* num3)
{
  add(num1, num2, num3);
  mul(num1, num2, num3);
}

void test2(unsigned char* num1, unsigned char* num2)
{
  add(num1, num2, num1);
}

void main() 
{
  int i,j ;

  test1(a,b,x);
  test2(c,y);

  for (i=0; i<4; i++)
  {
    printf("%d ",(x[i]);
  }
  printf(" ");

  for (i=0; i<4; i++)
  {
    printf("%d ", y[i]);
  }
}
5
  • What output do you expect?
    – mohit
    Aug 3, 2013 at 6:39
  • 2
    Welcome to Stack Overflow. Please read the About page soon. Please learn about creating an SSCCE (Short, Self-Contained, Correct Example). Please post compiling code: in function add(), you have num3[k] = num1[k] + num2[k]); and don't need the close parenthesis. This occurs several times — you should be able to fix that without needing to ask on SO. You have a stray open parenthesis at printf("%d ",(x[i]); before the x. Aug 3, 2013 at 6:40
  • There are extra )s on lines 14, 23, and 49. And don't use void main(), always use int main()
    – Yu Hao
    Aug 3, 2013 at 6:40
  • You have an off-by-one error in your add and mul functions. In both cases, the loop should go until k < 4. Aug 3, 2013 at 6:40
  • All comments and fixes consolidated under one community-wiki answer Aug 3, 2013 at 6:58

5 Answers 5

1

There were several compilation and logical errors :

  • Compilation errors :
    • Missing closing brace }
    • Missing closing bracket )
    • Extra closing bracket )
  • Logical errors :
    • Invalid array bounds check

Proper indentation goes a long way in easily identifying such issues with mis-matched brackets and braces to fix otherwise seemingly cryptic errors like "expected ; before ...".

Here is your corrected code with inline comments describing the errors and fix.

# include <stdio.h>

unsigned char a[4] = {5, 3, 6, 2}; 
unsigned char b[4] = {3, 5, 2, 5}; 
unsigned char c[4] = {4, 2, 5, 7}; 
unsigned char x[4];
unsigned char y[4];

void add(unsigned char* num1, unsigned char* num2, unsigned char* num3)
{
        int k;
        for (k=0; k<4; k++) {
                /* removed extra closing bracket */
                num3[k] = num1[k] + num2[k];
        }   
}

void mul(unsigned char* num1, unsigned char* num2, unsigned char* num3)
{
        int k;
        /* fixed value of array bounds check */
        for (k=0; k<4; k++) {
                num3[k] = num1[k] * num2[k];
        }   
}

void test1(unsigned char* num1, unsigned char* num2, unsigned char* num3)
{
        add(num1, num2, num3);
        mul(num1, num2, num3);
}

void test2(unsigned char* num1, unsigned char* num2)
{
        add(num1, num2, num1);
}

int main() 
{
        int i,j ;

        test1(a,b,x);
        test2(c,y);

        for (i=0; i<4; i++) {
                /* added missing closing bracket */
                printf("%d ",(x[i]));
        /* added missing closing brace */
        }   

        printf(" ");

        for (i=0; i<4; i++) {
                printf("%d ", y[i]);
        }   
}
3
  • 2
    The return type of main() should be int, even on Windows. Aug 3, 2013 at 6:40
  • @JonathanLeffler done. Feel free to make additional changes as deemed fit. Aug 3, 2013 at 6:42
  • @SohiniBasu Because it's against the standard.
    – Yu Hao
    Aug 3, 2013 at 8:14
0

It appears that x and y are uninitialized so passing y into test2 results in garbage values being passed into the second parameter of add.

0

Your array is 4 items, but you are using index <3

for (k=0; k<3; k++)

should be

for (k=0; k<4; k++)

And here you have an extra )

num3[k] = num1[k] + num2[k]);
num3[k] = num1[k] * num2[k]);
0

There is a parenthesis where there shouldn't be after num2[k], and change the loop they are in from < 3 to <=3. You missed a ( in printf( "%d ", (x[i]) )

0

num3[k] = (num1[k] + num2[k]); //fix bracket

printf("%d ",x[i]); //fix bracket

1
  • } is a closing brace. ) is simply a closing bracket Aug 3, 2013 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.