LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-29-2012, 07:29 AM   #1
errigour
Member
 
Registered: May 2009
Posts: 366

Rep: Reputation: 6
C math question


How come maxi equals -15?

Code:
#include <stdio.h>
#include <limits.h>
int main()
{
     int imin = INT_MIN; // minimum value
     int imax = INT_MAX; // maximum value
     int maxi, j;
     int pow(int a, int b)
     {
	  int sum, j = 0;
	  for ( j = 0; j < b; j++)
	       sum *= a;
	  return sum;
     }
     maxi += pow(2, 4) + 1;
     printf("%d:%d\r\n", sizeof (int), imin);
     printf("%d:%d\r\n", sizeof (int), imax);
     printf("%d:%d\r\n", sizeof (int), maxi);
}
 
Old 09-29-2012, 07:48 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by errigour View Post
How come maxi equals -15?
Look at every line that mentions maxi

Quote:
Code:
     int maxi, j;
...
     maxi += pow(2, 4) + 1;
...
     printf("%d:%d\r\n", sizeof (int), maxi);
}
The first line creates a local variable named maxi, uninitialized meaning the value could be anything.

The second line adds something to that unpredictable starting value yielding a still unpredictable value.

Since you make a similar error with sum inside the nested function, the result would still be unpredictable even if you fixed the first error.

Last edited by johnsfine; 09-29-2012 at 07:50 AM.
 
Old 09-29-2012, 07:49 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Consider initializing 'maxi' to something (e.g. zero). Also, sizeof() returns an unsigned long value; fix your printf() statements to use the formatter "%lu" for the sizeof() values.

Also, just so you know, your program does not conform to ISO C standards. You may want to consider placing your pow() function outside the scope of main(). Recompile your program using -pedantic as a compiler option to see the warning.

EDIT:

Also initialize 'sum' to some sane value in pow().

Last edited by dwhitney67; 09-29-2012 at 07:51 AM.
 
Old 09-29-2012, 07:55 AM   #4
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
How come maxi here yields -64

Code:
#include <stdio.h>
#include <limits.h>
int power(int a, int b)
{
     int sum, j = 0;
     for ( j = 0; j < b; j++)
     sum *= a;
     return sum;
}
int main()
{
     int imin = INT_MIN; // minimum value
     int imax = INT_MAX; // maximum value
     int maxi = 0;

     maxi = power(2, 6);
     printf("%lu:%d\r\n", sizeof (int), imin);
     printf("%lu:%d\r\n", sizeof (int), imax);
     printf("%lu:%d\r\n", sizeof (int), maxi);
}
 
Old 09-29-2012, 07:56 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Code:
...
int sum, j = 0;
...
Initialize 'sum' to zero.

And it should be:
Code:
...
sum += a;
...
Don't multiply, add.

EDIT: 'sum' should be multiplied, not added. Scratch my thoughts above.

Last edited by dwhitney67; 09-29-2012 at 08:01 AM.
 
Old 09-29-2012, 07:57 AM   #6
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
I thought int sum, j =0; initialized it but even after sum = 0 maxi ends up to be 0.
 
Old 09-29-2012, 07:58 AM   #7
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
why add I want to know what 2 * 2 * 2 * 2 * 2 * 2 is
 
Old 09-29-2012, 07:59 AM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by errigour View Post
I thought int sum, j =0; initialized it but even after sum = 0 maxi ends up to be 0.
You can initialize like this:
Code:
int sum = a, j = 0;


---------- Post added 09-29-12 at 08:59 ----------

Quote:
Originally Posted by errigour View Post
why add I want to know what 2 * 2 * 2 * 2 * 2 * 2 is
Dooh! You are right... I meant to initialize 'sum' to the value of 'a' (your first parameter).

Last edited by dwhitney67; 09-29-2012 at 08:00 AM.
 
Old 09-29-2012, 08:01 AM   #9
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

You should initialize sum to 1 (or to a if iterate to j<b-1), because sum is actually a product: sum = 1*a*a*a... You also do not need to initialize j, because it is initialized in the loop.

Last edited by firstfire; 09-29-2012 at 08:03 AM.
 
Old 09-29-2012, 08:01 AM   #10
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Wow I completly missed that

---------- Post added 09-29-12 at 09:02 AM ----------

sorry and thanx
 
Old 09-29-2012, 08:17 AM   #11
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by dwhitney67 View Post
Initialize 'sum'
Yes.

Quote:
to zero.
No.

Edit: Oops, I missed several posts despite refreshing the thread, so I gave an answer already given in more detail above.

Last edited by johnsfine; 09-29-2012 at 08:19 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Permissions a Question of Math Bob Fletcher Linux - Server 2 07-01-2012 12:07 AM
basic math question tinyTux General 8 08-15-2011 11:28 AM
math trick question xeon123 General 6 03-23-2007 06:54 AM
Help with math question nadroj General 28 01-20-2007 02:00 AM
Math question... counting theory Dark_Helmet General 6 08-30-2004 03:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration