Add to array together 
Author Message
 Add to array together

Hi,

I would like to add two arrays of 4000 data together in a loop.
//program not compiled
total[4000];
temp[4000];
FILE *finalfile;
int m, n, l;

for (n = 0; n < 4000; n++)
 {
  total[n] = 0;
 }
for (l = 0; l < 4000; l++)
 {
  total[l] = l;
 }
finalfile = fopen ("final.dat", "w");
for (m = 0; m<4000; m++)
  {
   fprintf (finalfile, "%d\n", total[m] = total[m] + temp [m]);
  }

Here the result will be :
0
0
1
0
2
0
3
0
instead of
0
1
2
3
...

Do you know what I did wrong??
Thanks.



Sun, 14 Oct 2001 03:00:00 GMT  
 Add to array together

   total[4000];
   temp[4000];
   FILE *finalfile;
   int m, n, l;

   for (n = 0; n < 4000; n++)
    {
     total[n] = 0;
    }
   for (l = 0; l < 4000; l++)
    {
     total[l] = l;
    }
   finalfile = fopen ("final.dat", "w");
   for (m = 0; m<4000; m++)
     {
      fprintf (finalfile, "%d\n", total[m] = total[m] + temp [m]);
     }

Why are you doing an assignment in the fprintf()?
Why are you using a different variable for the index in each loop?
Why are you referencing variables you have never initialized?
Why are you failing to check function return values?
--
(In need of an experienced ANSI C and cross-platform programmer?
 I'm seeking a programming internship for summer of 1999.)
Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead



Sun, 14 Oct 2001 03:00:00 GMT  
 Add to array together
<< //program not compiled >>

1. C++-style comment.

2. You should always compile your code before posting it.

<< Here the result will be ... >>

If you haven't compiled, how do you know this?

Do it this way:

#include <stdio.h>

int main()
{
 int total[4000];
 int temp[4000];
 FILE *finalfile;
 int m, n, l;

 for (n = 0; n < 4000; n++)
 {
  total[n] = 0;
 }
 for (l = 0; l < 4000; l++)
 {
  total[l] = l;
 }
 finalfile = fopen ("final.dat", "w");
 if(finalfile != NULL) {
  for (m = 0; m < 4000; m++)
  {
   fprintf (finalfile, "%d\n", total[m] + temp [m]);
  }
 }
 return 0;

Quote:
}

--

Paul Lutus
www.arachnoid.com

<snip>



Sun, 14 Oct 2001 03:00:00 GMT  
 Add to array together

Quote:

> Hi,

> I would like to add two arrays of 4000 data together in a loop.
> //program not compiled
> total[4000];
> temp[4000];
> FILE *finalfile;
> int m, n, l;

> for (n = 0; n < 4000; n++)
>  {
>   total[n] = 0;
>  }
> for (l = 0; l < 4000; l++)
>  {
>   total[l] = l;
>  }
> finalfile = fopen ("final.dat", "w");
> for (m = 0; m<4000; m++)
>   {
>    fprintf (finalfile, "%d\n", total[m] = total[m] + temp [m]);
>   }

> Here the result will be :
> 0
> 0
> 1
> 0
> 2
> 0
> 3
> 0
> instead of
> 0
> 1
> 2
> 3
> ...

> Do you know what I did wrong??
> Thanks.

Where do you give temp[m] a value?


Sun, 14 Oct 2001 03:00:00 GMT  
 Add to array together

Quote:

> Hi,

> I would like to add two arrays of 4000 data together in a loop.
> //program not compiled
> total[4000];
> temp[4000];
> FILE *finalfile;
> int m, n, l;

> for (n = 0; n < 4000; n++)
>  {
>   total[n] = 0;
>  }
> for (l = 0; l < 4000; l++)
>  {
>   total[l] = l;
>  }

1. I think that you meant that one of these two loops should fill temp
array.
2. There is no need in two different loop counters
3. It can be done into one loop

for( i=0; n<4000; n++ )
{
  total[ i ]  = 0;
  temp[ i  ]= i;

Quote:
}

> finalfile = fopen ("final.dat", "w");
> for (m = 0; m<4000; m++)
> {
>    fprintf (finalfile, "%d\n", total[m] = total[m] + temp [m]);

Try
fprintf( finalfile, "%d\n", total[m] + temp [m] );

- Show quoted text -

Quote:

> }

> Here the result will be :
> 0
> 0
> 1
> 0
> 2
> 0
> 3
> 0
> instead of
> 0
> 1
> 2
> 3
> ...

> Do you know what I did wrong??
> Thanks.



Mon, 15 Oct 2001 03:00:00 GMT  
 Add to array together

Quote:

> << //program not compiled >>

> 1. C++-style comment.

> 2. You should always compile your code before posting it.

> << Here the result will be ... >>

> If you haven't compiled, how do you know this?

> Do it this way:

> #include <stdio.h>

> int main()
> {
>  int total[4000];
>  int temp[4000];
>  FILE *finalfile;
>  int m, n, l;

>  for (n = 0; n < 4000; n++)
>  {
>   total[n] = 0;
>  }
>  for (l = 0; l < 4000; l++)
>  {
>   total[l] = l;
>  }
>  finalfile = fopen ("final.dat", "w");
>  if(finalfile != NULL) {
>   for (m = 0; m < 4000; m++)
>   {
>    fprintf (finalfile, "%d\n", total[m] + temp [m]);
>   }
>  }
>  return 0;
> }
> --

> Paul Lutus
> www.arachnoid.com


> <snip>

And where do YOU expect temp[m] to get a value?


Mon, 15 Oct 2001 03:00:00 GMT  
 Add to array together
Oops -- overlooked that error. The solution still works, if the assignments
are made correctly.

--

Paul Lutus
www.arachnoid.com

Quote:


>> << //program not compiled >>

>> 1. C++-style comment.

>> 2. You should always compile your code before posting it.

>> << Here the result will be ... >>

>> If you haven't compiled, how do you know this?

>> Do it this way:

>> #include <stdio.h>

>> int main()
>> {
>>  int total[4000];
>>  int temp[4000];
>>  FILE *finalfile;
>>  int m, n, l;

>>  for (n = 0; n < 4000; n++)
>>  {
>>   total[n] = 0;
>>  }
>>  for (l = 0; l < 4000; l++)
>>  {
>>   total[l] = l;
>>  }
>>  finalfile = fopen ("final.dat", "w");
>>  if(finalfile != NULL) {
>>   for (m = 0; m < 4000; m++)
>>   {
>>    fprintf (finalfile, "%d\n", total[m] + temp [m]);
>>   }
>>  }
>>  return 0;
>> }
>> --

>> Paul Lutus
>> www.arachnoid.com


>> <snip>

>And where do YOU expect temp[m] to get a value?



Mon, 15 Oct 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Add 2 char*'s together at run time

2. adding variables together in a function

3. Adding time together

4. Adding two strings together.

5. adding strings together

6. Multiply and Add matrices with 2d array WITHOUT ARRAY INDEXING

7. Question about adding an array to existing array.

8. Why does an array have no ADD method?

9. Adding new elements to array class instance?

10. PROBLEM TO ADD DATA IN AN ARRAY

11. add to array

12. Adding Arrays, anyone?

 

 
Powered by phpBB® Forum Software