Note that there are some explanatory texts on larger screens.

plurals
  1. POC - Adding an array to itself using dynamic arrays
    text
    copied!<p>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.</p> <p>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.</p> <p><strong>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.</strong></p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;conio.h&gt; int size; //global variable void ADDER(int *a, int *b) { int i; for (i = 0; i &lt; size; i++) { b[i] += a[i]; } } int main() { int n, i; printf("Enter the number of elements: "); scanf("%d", &amp;n); int *a = (int *)malloc(n*sizeof(int)); int *b; for (i=0; i&lt;n; i++) { printf("Enter element number %d: ", i); scanf("%d", &amp;a[i]); } ADDER(a,a); for (i=0; i&lt;n; i++) { printf("%d", a[i]); } } </code></pre> <p>Errors:</p> <p>1>------ Build started: Project: adderTest, Configuration: Debug Win32 ------</p> <p>1> adder.c</p> <p>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.</p> <p>1> e:\program files\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'</p> <p>1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(18): error C2143: syntax error : missing ';' before 'type'</p> <p>1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(19): error C2143: syntax error : missing ';' before 'type'</p> <p>1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(22): error C2065: 'a' : undeclared identifier</p> <p>1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(22): error C2109: subscript requires array or pointer type</p> <p>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'</p> <p>1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(24): warning C4024: 'ADDER' : different types for formal and actual parameter 1</p> <p>1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(24): error C2065: 'a' : undeclared identifier</p> <p>1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(24): warning C4047: 'function' : 'int *' differs in levels of indirection from 'int'</p> <p>1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(24): warning C4024: 'ADDER' : different types for formal and actual parameter 2</p> <p>1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(26): error C2065: 'a' : undeclared identifier</p> <p>1>e:\my documents\visual studio 2010\projects\addertest\addertest\adder.c(26): error C2109: subscript requires array or pointer type</p> <p>========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload