Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   programming with loops and break question. (http://www.go4expert.com/forums/programming-loops-break-question-t17634/)

ikj 21May2009 00:36

programming with loops and break question.
 
im writing a code where im multiplying 2 and 3 with their powers, ie (2 ^ i) * ( 3 ^ j )..n if the resultant sum is greater than 32 bits then im not storin that value in my array n so i check with the next powers ie if for some i n j the ans is less than the previous 1(since a value greater than 64bits gets truncated n the remainin number left after truncatin is less than the prev num) then im breaking n startin with the next i value...also while checkin if the answer is more than 64 bits i store it in the array only if isnt more than 64 bits..
the array should hold values like 0,1,2,3,4,6,8,9,12,18 etc n my code is not printing the values of 2 powers..
this is my code:
Code:


#include<iostream>
#include<math.h>
using namespace std;
 
main()
{
int c,n,d;
d=c=pow(32,2);
unsigned long a[d],b[c],t;
d=c=0;
a[d]=0;
b[c]=0;
for(int i=0;i<32;i++)
{
for(int j=0;j<32;j++)
{
b[c]=pow(2,i)*pow(3,j);
if(b[c]>b[c-1])
{
a[++d]=b[c++];
cout<<a[d]<<" ";
}
else
{
c--;
break;
}
}
}
}

i know its not very neat n all but i still dont know why my break line doesnt go to my first for loop...can u please tell me the error..
thanks for reading!

xpi0t0s 21May2009 02:28

Re: programming with loops and break question.
 
itreallyhelpsi
fyouusesom
ebasicgram
marcositsimp
ossibletowor
koutwtfyour
eonaboutatlea
stbreaklin
esattheendof
eachthoug
htifyouca
ntbearsedw
ithcapitallette
rsandfullsto
ps.

xpi0t0s 21May2009 02:29

Re: programming with loops and break question.
 
ohandusefsckingcode
blockthereisagreatbighint
rightwhereyoupost
anewthreadthatssodam
nobviousyoumustbeposi
tivelytryingtoignorestuff.

ikj 21May2009 07:36

Re: programming with loops and break question.
 
Code:


#include<iostream>
#include<math.h>
using namespace std;
 
main()
{
int c,n,d;
/*math.pow returns a double
good idea to cast it to a u int, just for the sake of it*/
d=c=(unsigned long)(pow(32,2));
unsigned long a[d],b[c],t;
d=c=0;
a[d]=0;
b[c]=0;
for(int i=0;i<32;i++)
{
printf(".");
for(int j=0;j<32;j++)
{
b[c]=(unsigned long)(pow(2,i)*pow(3,j));
/*This is your primary problem.
consider when j = 0, on a new loop, this will not be false every time, since the previous entry was
at y = 32 or some such.
Easier fix, just ignore this rule when j = 0, as below. This is not always a totally safe assumption, but
neither is this method of overflow checking, so it ought to do the job.*/
if(j==0 || b[c]>b[c-1])
{
a[++d]=b[c++];
}
else
{
/*c--; */
/*Decrementing this means the next value written will overwrite the previous VALID entry.
You just need to not increment c.*/
break;
}
}
}
for(int k=0;k<d;k++)
cout<<a[k]<<endl;
}


ikj 21May2009 07:44

Re: programming with loops and break question.
 
hey. sorry bout that.
what i want to do is to print the different results of (2^i)*(3^j) with i and j both from 0 to 32, which will be something like this: 1,2,3,4,6,9,12,16, and so on, but obviously not in the sorted order(i'll sort it later). whats happening now with my program is that its not printing the values of any of the powers of 3. so could you please help me correct that!
thanks,here's the code:

Code:

#include<iostream>
#include<math.h>
using namespace std;
 
main()
{
 int c,n,d;
 d=c=(unsigned long)(pow(32,2));
 unsigned long a[d],b[c],t;
 d=c=0;
 a[d]=0;
 b[c]=0;
 for(int i=0;i<32;i++)
 {
  printf(".");
  for(int j=0;j<32;j++)
  {
    b[c]=(unsigned long)(pow(2,i)*pow(3,j));
    if(j==0 || b[c]>b[c-1])
    {
        a[++d]=b[c++];
      }
      else
      {
        break;
      }
  }
}
  for(int k=0;k<d;k++)
      cout<<a[k]<<endl;
}


xpi0t0s 21May2009 12:45

Re: programming with loops and break question.
 
Thanks, that's much more readable.

It seems to work OK on my system. Changing the loop limits to 6 (instead of 32, and just to limit the total output for debugging purposes) I get the following output:
Code:

......0
1
3
9
27
81
243
2
6
18
54
162
486
4
12
36
108
324
972
8
24
72
216
648
1944
16
48
144
432
1296
3888
32
96
288
864
2592

If you limit the loops to 6, what output do you get?

With an upper limit of 32 this is the first part of the output for me (not incl the .....0) :
Code:

1
3
9
27
81
243
729
2187
6561
19683
59049
177147
531441
1594323
4782969
14348907
43046721
129140163
387420489
1162261467
3486784401
2
6
18
54
162
486
1458
4374
13122
39366
118098
354294
1062882
3188646
9565938
28697814
86093442
258280326
774840978
2324522934
2678601506
3740837222

which seems to be in order (i.e. correct, as opposed to sorted, which it clearly isn't).

ikj 21May2009 23:23

Re: programming with loops and break question.
 
tcaseshanks..
but you know some of the answers are wrong, only a few though.
its like this: my compiler is of 32 bits, and in my code im checking if the storage capabilty exceeds 32 bits by checking whether the answer is less than or greater than the previous answer; and im not storing the lesser ones( since they have been truncated:
eg with ((2^64)-10)+19=(2^64)+9=0+9=9 (since 2^64 is basically one followed by 64 zeroes where the one at the front is discarded due to the system restrictment)

but after thinking about it you might realise that my method is correct if you're adding two numbers n their sum is greater than 32 bits, because if the answer is greater than 32 bits then the front part is truncated leaving the remaining bits which is stored, and on multiplying for some cases the number remaining after truncating is still greater than the previous number so its getting stored which shudn't happen. so how do i go about correcting that?
or atleast could you tell me how to check overflow while multiplying two numbers in c++ coz what ive done is for addition and not for multiplication?

ikj 21May2009 23:24

Re: programming with loops and break question.
 
the first line shud've been thanks.

xpi0t0s 22May2009 04:40

Re: programming with loops and break question.
 
You just need to think about it slightly differently. Obviously you can't say if x>=2^32-1 because that's always going to evaluate FALSE. Let's just bring the numbers down a bit so we don't get lost in all the digits; suppose x+y has to be less than 10, and we can't say "if x+y>=10" due to overflow. Let's say x is 6. What are the possible values of y, and how do you imagine that could be figured out?

ikj 22May2009 08:51

Re: programming with loops and break question.
 
hey thanks...
so u meant go backward from the maximum possible value right?
well i tried this:
Code:


if(j==0 || ULONG_MAX/pow(3,j)>pow(2,i))
{
                a[++d]=b[c++];
                cout<<a[d]<<" ";
}
else
{
                  break;
}

(ULONG_MAX is from<climits>)
and it works perfectly!
thanks alot!


All times are GMT +5.5. The time now is 10:32.