1

I started a different thread for this, I tried solving it using the help they gave me but I am not able to run the program. Can anyone tell me what is wrong in the program and how is it supposed to be? Thanks.

The program is supposed to add an array to itself and replace the original with the sum, so when the initial array is printed, it prints the sum. This is what I have done so far.

Please note it is a must to use ADDER(a,a) as a function call. I am not allowed to change that. Both parameters are to be passed by reference.

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int size; //global variable

void ADDER(int *a, int *b) {
    int i;
    for (i = 0; i < size; i++) {
        b[i] += a[i];
    }     
}

int main() {
    int n, i;
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    int *a = (int *)malloc(n*sizeof(int));
    int *b;
    for (i=0; i<n; i++) {
        printf("Enter element number %d: ", i);
        scanf("%d", &a[i]);
    }
    ADDER(a,a);
    for (i=0; i<n; i++) {
        printf("%d", a[i]);
    }
}

Errors:

1>------ Build started: Project: adderTest, Configuration: Debug Win32 ------

1> adder.c

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(17): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

1> e:\program files\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(18): error C2143: syntax error : missing ';' before 'type'

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(19): error C2143: syntax error : missing ';' before 'type'

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(22): error C2065: 'a' : undeclared identifier

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(22): error C2109: subscript requires array or pointer type

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(24): error C2065: 'a' : undeclared identifier 1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(24): warning C4047: 'function' : 'int *' differs in levels of indirection from 'int'

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(24): warning C4024: 'ADDER' : different types for formal and actual parameter 1

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(24): error C2065: 'a' : undeclared identifier

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(24): warning C4047: 'function' : 'int *' differs in levels of indirection from 'int'

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(24): warning C4024: 'ADDER' : different types for formal and actual parameter 2

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(26): error C2065: 'a' : undeclared identifier

1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(26): error C2109: subscript requires array or pointer type

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

8
  • 2
    What does it do instead of what it is supposed to do? Commented Jan 3, 2012 at 19:02
  • 2
    You don't need to cast the return value of malloc Commented Jan 3, 2012 at 19:02
  • @Scott it does not run, gives a lot of errors. Commented Jan 3, 2012 at 19:04
  • To b or not to b, unless there's a good reason, you don't even really need to pass two array arguments to adder if the assumption is that it's always adding the array to itself, just a[i] += a[i] would suffice and leave b out of it. Commented Jan 3, 2012 at 19:09
  • Please note it is a must to use ADDER(a,a) as a function call. I am not allowed to change that. Commented Jan 3, 2012 at 19:11

3 Answers 3

7

You have a size object in both file scope and block scope (in main function).

To fix your program you can remove the definition of the size object in the main function or (preferred solution) remove the one in file scope and pass it as a new argument to your ADDER function.

If you compile with gcc you can use the -Wshadow warning that will alert you when a variable shadows another one.

Sign up to request clarification or add additional context in comments.

7 Comments

also, int*b is not used in main, and you should not generally use globals anyway, rather, pass size to your adder function, or consider using a struct that has both the int[] and the size if you like OO.
Just to explain this a little more: this will cause the global size variable to have a random value (frequently 0), which will cause the loop in ADDER to do nothing.
@ChuckBlumreich int objects defined at file scope are always initialized to 0 if not explicitly initialized
Please note it is a must to use ADDER(a,a) as a function call. I am not allowed to change that.
@ouah You're right that an int will always be initialized to 0. I remember the "good old days" when that wasn't the case - that was only guaranteed while in debug builds. So, I always assume a random value for anyhting not explicitly set. Old habits die hard, I guess.
|
5

The biggest problem I see is that you have 2 size variables

  • The global size
  • The local size declared in main

The ADDER function uses the global version of size which is never assigned to hence it functions incorrectly. The easiest fix is to avoid the global and just pass along the size to ADDER.

void ADDER(int *a, int *b, int size) {
 ...
}

2 Comments

but I have to use ADDER(a,a) that is a must requirement. I cannot pass any other variable.
@HelloWorld in that case you need to change main to use the global size
2
#include <stdlib.h>
#include <stdio.h>

/* global variable to store the size */
int g_size;

void ADDER(int *a, int *b) 
{
    int i;
    for (i=0 ; i<g_size ; i++) {
        b[i] += a[i];
    }

    return;     
}

int main(void) 
{
    int n, i;

    printf("Enter the number of elements: ");
    scanf("%d", &n);

    int *a = NULL;
    if ((a = (int *)malloc(n * sizeof(int))) == NULL) {
        printf("unable to allocate memory \n");
        return -1;
    }

    for (i=0; i<n; i++) {
        printf("Enter element number %d: ", i);
        scanf("%d", &a[i]);
    }

    g_size = n;
    ADDER(a, a);

    for (i=0; i<n; i++) {
        printf("%d \n", a[i]);
    }

    free(a);
    return 0;
}

I made the following modifications:

  • error checking for malloc()
  • once done, free()'ed the memory
  • formatted output
  • removed conio.h as it is not needed
  • and, finally made it work! :)

EDIT: Update the code as per the request of the OP author.

5 Comments

neat .. thanks! but the problem states that I should not pass the size of the array via ADDER. Can it be done without passing the size?
@HelloWorld OK, I didn't see that in the OP but however that can be achieved by your technique of global variables. But please ensure, you assign the size i.e. n to the global variable size before calling ADDR. If you need the modified program, I can do another post.. let me know!
Yeah I tried that using global variables but didn't quite get it right. Can you edit your current post with the modified program? :)
@HelloWorld Done! If you find my answer satisfying, please confirm the same by clicking the tick mark next to it! .. Hope this helps!! Thanks!!
Thanks! I'm trying to test it but the output window is closing before it displays the result! :(

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.