Solved

dats goes missing

Posted on 1998-11-11
24
167 Views
Last Modified: 2010-04-15
where dos the first line of data go? I can get over this problem a few ways, but would like to know.
also sugest any inprovments.



#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<iomanip.h>

float main()
{
register int i, j, count;
       float **grid;
       int x, y, k, print = 0, test = 0;
       cout << "Define the size of the grid ie. x y :";
       cin >> x >> y;

       grid = (float **)calloc(x, sizeof(float *));

       for(k=0; k < x; ++k)
       grid[k] = (float *)calloc(y, sizeof(float));

       cout << "Enter the values" << endl;
       for(j = 1; j < (y - 1); j++)
            cin >> grid[0][j];

       clrscr();
       cout << "processing please wait..." << "\n\n\n";

      for(count = 0; count < 100; count++){
            for(i = 1; i < (x - 1); i++){
                  for(j = 1; j < (y - 1); j++){
                  grid[i][j] = .25 * (grid[i + 1][j] + grid[i - 1][j] +
                  grid[i][j + 1] + grid[i][j - 1]);
                        if(print == 1){
                        cout << setiosflags(ios::fixed) << grid[i][j] << "  ";
                        }
                  }
            if(print == 1){
            cout << "\n";
            }

                  if(count >= 99){
                  print = 1;
                  }
            }
      }

return 0;
}
0
Comment
Question by:mayobh
  • 7
  • 7
  • 7
  • +1
24 Comments
 

Author Comment

by:mayobh
Comment Utility
Edited text of question
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I think your problem is in the for loops

for(i = 1; i < (x - 1); i++){
for(j = 1; j < (y - 1); j++){

Why do you start the counter at 1 instead of 0?  I suspect that is the problem.  In additon, why do you end the loops "early"?
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
What do you "mean where the data go"?
It looks to me like they diffuse into the rest of the grid.

nietod, I presume the counter is started at 1 so that grid[i - 1] stays in bounds
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Or are you asking why
 print = 1;
is set at the bottom of the loop rather than at the top?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> I presume the counter is started at 1 so that grid[i - 1] stays in bounds
I totally missed that.
0
 

Author Comment

by:mayobh
Comment Utility
yes the loop starts at 1 because the outside of the grid is grounded the program is ment to solve lapances equation for a grid of resistors.

if i do this       if(count >=98)
so it will print two soluations it only prints 6 lines the first time and 7 (the amount i need) the seconde. my question is where is that line of data going
the first line should be if grid = 9  9
and the inputs are all 5

2.413    3.305    3.627    3.7416  etc
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
Comment Utility
Hi, i make some changings in you code (in this lines
i write "changed") and now program works(can be).
Main changing: as nietod wrote, INPUT to array from Screen
must make from 0!

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<iomanip.h>

float main()
{
  register int i, j, count;
  float **grid;
  int x, y, k, print = 0;
  cout << "Define the size of the grid ie. x y :";
  cin >> x >> y;

  grid = (float **)calloc(x, sizeof(float *));

  for(k=0; k < x; ++k)
    grid[k] = (float *)calloc(y, sizeof(float));

  cout << "Enter the values" << endl;
  for(j = 0; j < y; j++)            //changed
    cin >> grid[0][j];

  clrscr();
  cout << "processing please wait..." << "\n\n\n";

  for(count = 0; count < 100; count++)
    {
      if (count >= 99)            //changed
       print = 1;
     for(i = 1; i < (x - 1); i++)
       {
       for(j = 1; j < (y - 1); j++)
         {
           grid[i][j] = .25 * (grid[i + 1][j] + grid[i - 1][j] +
                  grid[i][j + 1] + grid[i][j - 1]);
           if (print == 1)
            cout << setiosflags(ios::fixed) << grid[i][j] << "    ";//changed
         }
       if (print == 1)
           cout << "\n";
       }
    }
  getch();
  return 0;
}
Regards, Alex
0
 

Author Comment

by:mayobh
Comment Utility
yes this fixes the problem (but   for(j = 1; j < (y - 1); j++)  must stay the same)
but I want to know why there is a problem with putting the asigment at the end of the loop.
So I must say no, you will get the points if no one can explain.
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
Comment Utility
What sort of explanation you want? You use variable grid[0][0],
but in this variables you not put any value(and can be garbage).
It is error, that must fixe!
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Where do you use variable grid[0][0]?  I must have missed something.

If you put the print = 1; assignment at the bottom of the loop,
then the first time through, it will not have been set when (print == 1) is tested earlier in the loop.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> Where do you use variable grid[0][0]?  I must have missed something.

When he initializes the values by getting input from cin, he only initializes the "interior.  That is the top and bottom rows and left and right columns are never intialized.    Then in the later calculations, he iterates over these "interior" points (never these bounding points), but he uses the values in the "bounding" points in the calculations.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
calloc initializes to zero (which can correspond to (float)0.0 on some architectures)
But what does that have to do with mayobh's question?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> calloc initializes to zero (which can correspond to (float)0.0 on some architectures)
It might, but not ussually though.
>>But what does that have to do with mayobh's question?
Its a problem, not the problem.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
I'd consider IEEE floating point representation to be usual.
Nor is all zeros representing 0.0 unusual among non-IEEE formats.
It can be a potential problem, especially if the code is meant to be portable,
But in any case, it will be some fixed contant value, which may be all that's required.
(and I still don't see where grid[0][0] is ever used)
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>>I'd consider IEEE floating point representation to be usual.
If I remember corrently, all 0 byte would not be 0 in IEEE.  I think that would be a NAN or undefined value of some type.  the exponent is not a stored as a signed number, but sort of as an offset from some negetave minimum exponent.  (i.e if there is a 1 byte exponent, then that "real" expononent is considered to be -128 plus the "offset" stored.)  The only way to represent 0 in IEEE is with a 0 mantissa and a 0 exponent (the real exponent, not the offset).  I could be wrong,b ut that is the way I remember it.  A 0 mantissa and non-zero exponent is used to represent all the special values, like NANs, infinity etc.

>>I still don't see where grid[0][0] is ever used)
[0][0] is not used (nor any corners), but the other edge values are used in the calculations.  Each point is calculated using the adjacent points.  He only calculates the interior points, but when he calculates points that are 1 row/column in from the edge, it uses points that are along the edge
0
 

Author Comment

by:mayobh
Comment Utility
Sorry for not getting back to the contributors quicker, I am changing systems and have been off the net for a few days

I tough that calloc would innisolize the grid to 0 for me.
And no one has yet tackled my question except ozo, but the fact remains if I set the code to say print the results for two iteration of the problem the first one will contain 1 less line of date which is required (the first one) where the second will have a full complement of results. I know there are meny ways to fix this in fact it only cane about when I was making a change to the code at the start, I saw the problem and could not understand why it was a problem.

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> And no one has yet tackled my question except ozo, but the fact
>> remains if I set the code to say print the results for two iteration
>> of the problem the first one will contain 1 less line of date
Ozo solved this problem.  do you realize that?   It is the position of your code that tests if the count is 99 and sets print to 1 if so.  That is in the wrong place.  it sets print to 1 part way through the calculations on the 99th interation, so the first line isn't printed.  Move that to the top and you will be fine, like

for(count = 0; count < 100; count++){
   if(count >= 99){
      print = 1;
   }
    for(i = 1; i < (x - 1); i++){
      for(j = 1; j < (y - 1); j++){
         grid[i][j] = .25 * (grid[i + 1][j] + grid[i - 1][j] +
         grid[i][j + 1] + grid[i][j - 1]);
         if(print == 1){
            cout << setiosflags(ios::fixed) << grid[i][j] << "  ";
         }
      }
      if(print == 1){
         cout << "\n";
      }
   }
}
0
 

Author Comment

by:mayobh
Comment Utility
Why when the statement is outside the two for loops, dos it not run trough the two for loops, set print to 1 and then print the full set of solutions for the problem, on the next set of for loops.

I do not wish to take away form ozo but that was not my question he answered.


0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
the print = 1; statement is not outside the two for loops in your original program
it is inside the for(i = 1; i < (x - 1); i++){
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
Comment Utility
ozo,nietod, i think, that it is time to end a discassion .
I fix errors, mayobh do'es understand.
Ozo write explanation: mayobh do'es understand.
Nietod write explanation of ozo reply : mayobh do'es understand. How many times you can make it?!


0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
The problem was that it set print to 1 on the 99th loop after it had the opportunity to print the first line, but before it had the opportunity to print the 2nd line.  Thus you need to set print to 1 before it starts the calculations and printing for a particular iteration.


0
 

Author Comment

by:mayobh
Comment Utility
Ok. I capitulate and generally role over and play dead. Time for an answer submission.
0
 

Author Comment

by:mayobh
Comment Utility
form ozo
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
Comment Utility
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<iomanip.h>

int main()
{
   register int i, j, count;
   float **grid;
   int x, y, k, print = 0, test = 0;
   cout << "Define the size of the grid ie. x y :";
   cin >> x >> y;

   grid = (float **)calloc(x, sizeof(float *));

   for(k=0; k < x; ++k)
       grid[k] = (float *)calloc(y, sizeof(float));

   cout << "Enter the values" << endl;
   for(j = 1; j < (y - 1); j++)
       cin >> grid[0][j];

   clrscr();
   cout << "processing please wait..." << "\n\n\n";

   for(count = 0; count < 100; count++){
       if(count >= 99){
           print = 1;
       }
       for(i = 1; i < (x - 1); i++){
           for(j = 1; j < (y - 1); j++){
               grid[i][j] = .25 * (grid[i + 1][j] + grid[i - 1][j] +
               grid[i][j + 1] + grid[i][j - 1]);
               if(print == 1){
                   cout << setiosflags(ios::fixed) << grid[i][j] << "  ";
               }
           }
           if(print == 1){
                cout << "\n";
           }
       }
   }

   return 0;
}

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

744 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

13 Experts available now in Live!

Get 1:1 Help Now