Solved

file processing

Posted on 1998-10-19
16
162 Views
Last Modified: 2012-05-04
i have stock.dat file ( two columns of stocks) and i created the program to read from this file and display two columns of stocks, but i cant figure out how to get the average of each stocks.  actually i have two arrays of stocks with size 20, but when i try to create a function to compute the average i get incorrect answer.   i would be gratitude if you help me with this problem
0
Comment
Question by:elina
  • 7
  • 5
  • 2
  • +2
16 Comments
 
LVL 3

Expert Comment

by:rmichels
ID: 1253683
Can you post your source code for your average function?

0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1253684
total=0;
for (j=0; j<20; j++) total += stock[j];
avg=stock/20;
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1253685
i mean avg=total/20;

0
 
LVL 10

Accepted Solution

by:
rbr earned 100 total points
ID: 1253686
What incorrect answer to you get. What types are stock and total. If they are short integer you got an overrun maybe. Use long or better double.
0
 

Author Comment

by:elina
ID: 1253687
thanks for all replies.  here is the original code. Average ()doesnt work correctly.
thanks a lot
Elina


#include <stdio.h>
#define SIZE 20

void Average(char [], int);

main()
{
      int i;
      char stock1[SIZE];
      char stock2[SIZE];
      FILE *cfPtr;


      if ((cfPtr = fopen ("STOCK.DAT","r")) == NULL)
            printf("file couldnt be opened\n");

      else {
            printf("%s%\t\t%s\n", "stock 1", "stock 2");
            fscanf(cfPtr, "%s%s", stock1, stock2);

            while (!feof (cfPtr)) {
                  printf("%s\t\t%s\n", stock1, stock2);
                  fscanf(cfPtr, "%s%s", stock1,  stock2);
            }

      }

          Average (stock1, SIZE );
      
            fclose (cfPtr);


            return 0;
}

      void Average(char b[], int size)
{
      int j, temp;
      int  total;
      for (j = 0; j <= size; j++)
      temp += b[j];
      total = temp / 20;
      printf("total %d\t\n", b[j]);

}

      
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1253688
void Average(char b[], int size)
{
int j, temp;
int  total;
for (j = 0; j <= size; j++)
temp += b[j];
total = temp / 20;
printf("total %d\t\n", total)      //this line changed!!!
}

In the printf statement you were printing the last element of the array (in b[j]) instead of the average (in total).
0
 

Author Comment

by:elina
ID: 1253689
the average is still incorrect and i cant figure out why.  i got big numbers like 3456789

0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1253690
Initialize temp and total to 0.

int temp=0,
int total=0
0
New! My Passport Wireless Pro Wi-Fi Mobile Storage

Portable wireless storage to offload, edit, and stream anywhere.

High-capacity, wireless mobile storage designed to accompany professional photographers and videographers in the field to easily offload, edit and stream captured photos and high-definition videos.

 
LVL 5

Expert Comment

by:scrapdog
ID: 1253691
Also change temp=temp/20 to temp=temp/size

This will accomplish the same thing, but will allow the average function to accept arrays other than a size of 20.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1253692
If rbr's answer didn't work why did you accept it?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1253693
What are you reading into stock1?
It looks like it's the characters from your last read of cfPtr
0
 

Author Comment

by:elina
ID: 1253694
thanks guys but it still doesnt work.  
for ozo:  i am reading from stock1 and trying to get average
for scrapdog: about answer. i didnt pay attention that i checked it.  i am new here.  
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1253695
Please repost your revised source code.


0
 

Author Comment

by:elina
ID: 1253696
#include <stdio.h>
#define SIZE 20

void Average(float []);

main()
{
      int i;
      float stock1[SIZE];      
      float stock2[SIZE];
      FILE *cfPtr;

      if ((cfPtr = fopen ("stock.dat","r")) == NULL)
            printf("file couldnt be opened\n");

      else {
            printf("%s\t%s\n", "stock 1", "stock2");        
            fscanf(cfPtr, "%s\t%s", stock1, stock2);
                                                   
            while (!feof (cfPtr)) {
                  
                  printf("%s\t%s\n", stock1, stock2);            
                  fscanf(cfPtr, "%s\t%s", stock1,stock2);
            }
      }
          Average (stock1);      
            fclose (cfPtr);

            return 0;
}
      void Average(float b[])
{
      int j;
      float  temp, total = 0;
      for (j = 0; j <= SIZE; j++)
      temp = b[j] + total;
      total = temp / SIZE;  // i get '0' here
      printf("total %d\t\n", total);

}

0
 
LVL 84

Expert Comment

by:ozo
ID: 1253697
That's a little better, but you're still only reading into stock1[0] and stock2[0], whereas you try to average from 21 elements
(even though they are only declared to contain 20 elements)

also, you're telling fscanf that stock1 and stock2 are character pointers, when they are actualy arrays of float


     for( i=0; i<SIZE; i++ ){ stock1[i]=0; stock2[j]=0; }
     if ((cfPtr = fopen ("stock.dat","r")) == NULL)
     printf("file couldnt be opened\n");

     else {
       i = 0;
     while ( i < SIZE && !feof (cfPtr)) {
     fscanf(cfPtr, "%f\t%f", &stock1[i],&stock2[i]);
     printf("%f\t%f\n", stock1[i], stock2[i]);
     i++;
     }
     }
          Average (stock1);
     fclose (cfPtr);

     return 0;
     }
     void Average(float b[])
     {
     int j;
     float  temp, total = 0;
     for (j = 0; j < SIZE; j++)
     total = b[j] + total;
     total = total / SIZE;  /*// i get '0' here */
     printf("total %f\t\n", total);



0
 

Author Comment

by:elina
ID: 1253698
after small changes it works perfectly
thanks a lot  
elina

#include <stdio.h>
#define SIZE 20

void Average(float []);

main()
{
      int i;
      float stock1[SIZE];      
      float stock2[SIZE];
      FILE *cfPtr;

     if ((cfPtr = fopen ("stock.dat","r")) == NULL)
     printf("file couldnt be opened\n");

     else {
          printf("%s\t%s\n", "stock 1", "stock2");        
            fscanf(cfPtr, "%f\t%f", stock1, stock2);
        i = 0;
     while ( i < SIZE && !feof (cfPtr)) {
            fscanf(cfPtr, "%f\t%f", &stock1[i],&stock2[i]);
            printf("%.2f\t%.2f\n", stock1[i], stock2[i]);
            i++;
            }
     }
     Average (stock1);
     fclose (cfPtr);

     return 0;
     }
     void Average(float b[])
     {
     int j;
     float total = 0;
        for (j = 0; j < SIZE; j++)
            total += b[j];
            total = total / SIZE;
            printf("total %.2f\t\n", total);
       }



0

Featured Post

Free camera licenses with purchase of My Cloud NAS

Milestone Arcus software is compatible with thousands of industry-leading cameras for added flexibility. Upon installation on your My Cloud NAS, you will receive two (2) camera licenses already enabled in the software. And for a limited time, get additional camera licenses FREE.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now