Solved

dats goes missing

Posted on 1998-11-11
24
170 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
ID: 1254187
Edited text of question
0
 
LVL 22

Expert Comment

by:nietod
ID: 1254188
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
ID: 1254189
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 84

Expert Comment

by:ozo
ID: 1254190
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
ID: 1254191
>> 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
ID: 1254192
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
ID: 1254193
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
ID: 1254194
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
ID: 1254195
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
ID: 1254196
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
ID: 1254197
>> 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
ID: 1254198
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
 
LVL 22

Expert Comment

by:nietod
ID: 1254199
>> 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
ID: 1254200
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
ID: 1254201
>>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
ID: 1254202
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
ID: 1254203
>> 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
ID: 1254204
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
ID: 1254205
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
ID: 1254206
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
ID: 1254207
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
ID: 1254208
Ok. I capitulate and generally role over and play dead. Time for an answer submission.
0
 

Author Comment

by:mayobh
ID: 1254209
form ozo
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 1254210
#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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

810 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