• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

dats goes missing

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
mayobh
Asked:
mayobh
  • 7
  • 7
  • 7
  • +1
1 Solution
 
mayobhAuthor Commented:
Edited text of question
0
 
nietodCommented:
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
 
ozoCommented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
ozoCommented:
Or are you asking why
 print = 1;
is set at the bottom of the loop rather than at the top?
0
 
nietodCommented:
>> I presume the counter is started at 1 so that grid[i - 1] stays in bounds
I totally missed that.
0
 
mayobhAuthor Commented:
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
 
AlexVirochovskyCommented:
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
 
mayobhAuthor Commented:
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
 
AlexVirochovskyCommented:
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
 
ozoCommented:
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
 
nietodCommented:
>> 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
 
ozoCommented:
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
 
nietodCommented:
>> 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
 
ozoCommented:
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
 
nietodCommented:
>>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
 
mayobhAuthor Commented:
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
 
nietodCommented:
>> 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
 
mayobhAuthor Commented:
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
 
ozoCommented:
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
 
AlexVirochovskyCommented:
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
 
nietodCommented:
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
 
mayobhAuthor Commented:
Ok. I capitulate and generally role over and play dead. Time for an answer submission.
0
 
mayobhAuthor Commented:
form ozo
0
 
ozoCommented:
#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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 7
  • 7
  • 7
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now