Amjad
Amjad

Reputation: 1687

C - Adding an array to itself using dynamic arrays

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 ==========

Upvotes: 1

Views: 919

Answers (3)

Sangeeth Saravanaraj
Sangeeth Saravanaraj

Reputation: 16597

#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.

Upvotes: 2

ouah
ouah

Reputation: 145829

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.

Upvotes: 7

JaredPar
JaredPar

Reputation: 754585

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) {
 ...
}

Upvotes: 5

Related Questions