ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Click Here to receive this Complete Guide absolutely free.

I appreciate your help jpollard, and I'm sure your right in what you're saying, but it's just getting more and more beyond me at this point. I have no idea why I need to do any of the steps you guys talk about other than it converts it from decimal to hex. I'm not trying to be funny or anything, but it's just beyond me.

It is necessary to understand how to follow an algorithm; this is how a computer steps through a program. EVERY step is important for achieving the goal. Understanding base arithmetic is the first step. The details are important as leaving any step out, or doing them in the wrong order, causes failures.

Will you use this frequently? not often. This specific function is used often enough that it is included as a format specification in printf; as is base 8 notation and base 10 notation. But sometimes doing conversions is necessary. Like if you are trying to print money values... you have special goal - like putting commas every three digits... using scaled values sometimes (which is part of base conversion. Conversion to hex is scaling a value by 16 at every step... But to preserve the information about the scale of the resulting number means you have to do something with the remainders... but the specific thing to do depends on the goal of the scaling).

Numbers inside the processor are always base 2. So any conversion to something else is a base conversion. Using compression is a form of base conversion... So is encryption/decryption (though much more complex than basic number bases).

Everything a computer can do is based on mathematics. It is just the application of math to things not usually considered math that makes it hard to recognize the math. Even doing things like the fonts used for showing things is math - at the simplest, a matrix of dots. But that doesn't look good when scaled... so there are also scalable fonts - which are tables of math parameters to describe combinations of curves and define "inside" and "outside" so that some areas can be filled in, and other areas not filled in. Scaling these to any size (there is a minimum size though) makes the resulting display look good.

Like learning to balance a checkbook, you first have to learn arithmetic. It is just getting used to tracking what is being done, recognizing when side effects can be used (like the remainder is a side effect of division).

One thing that might help with base conversion is writing a program that does the base conversion. Using basic operators of "/" for division and "%" for modulo to get a remainder, is easier than doing long division. But the long division is what the computer uses to get these results of the "/" and "%" operations.

And like many problems, there are special cases that make shortcuts available. Hex and octal conversions produce results that will fit with a specific number of bits (hex, 4 bits, octal 3 bits). So base conversions of these are easy if the original base is 2 (which is what the computer uses). For base 16, every 4 bits of the original value is the desired "remainder". For base 8, every 3 bits. So for these special cases, a different algorithm can be used for the conversion.

Quote:

While it's probably a waste of time asking the following question given the above, I'll ask it anyway;

In the C pdf, it says the following for one of the exercises at the end of the "Program Looping" chapter;

Quote:

2. Write a program to generate and display a table of n and n2, for integer values of n
ranging from 1 to 10. Be certain to print appropriate column headings.

The last part is obvious and was fairly easy, but does the "n2" (with the small "2") mean an "exponent", and therefore n x n ?

A variable name in C is any sequence of characters composed of "a" to "z", "A" to "Z", and the character "_" (which is conventionally used for "special" use as a first character) for the very first character of the variable name.
The rest of the characters for a name can ALSO include the digits "0" through "9".

So n2 is a variable name.

The only other point is that C doesn't include an exponentiation operator. There is a function for that in the math library for real numbers.

Quote:

I've posted my code below, but if the question above does mean "n2" as in an "exponent", then I don't think my code below does that, and I have no idea how to get it to do that. So I'm not even sure what the question is even asking me to do.

Code:

#include <stdio.h>
int main(void)
{
int number, plusnumber = 0;
printf("Table of Triangular Numbers\n");
printf("number Sum of 2 to number\n");
printf("------ ------------------\n");
for( number = 1; number <= 10; ++number) {
plusnumber += number;
printf("%2i %2i\n", number, plusnumber);
}
return 0;
}

Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.

Distribution: CentOS at the time of this writing, but some others over the years too...

Posts: 2,546

Original Poster

Rep:

Thanks again for your help jpollard. I just don't know what to do. I'm not trying to digress or anything I'm honestly not, and I don't want to waste anyone's time. But I'm clearly no good at maths, I did honestly try, but it's just pissing me off not understanding it. And given what you guys have said about the need to understand it, and I can see why the more I read the C pdf/book, it's starting to look like I just don't have the maths knowledge required for it. And I'm probably gonna be dead by the time I ever can figure it out. So I'm just not sure it's worth trying to continue with it, I do want to learn how to program, but like I said, it's just getting too hard to try and figure the maths out.

I tried to do the following, but it doesn't matter what I try, it just won't work;

Quote:

3. A triangular number can also be generated by the formula
triangularNumber = n (n + 1) / 2
for any integer value of n. For example, the 10th triangular number, 55, can be
generated by substituting 10 as the value for n in the preceding formula.Write a
program that generates a table of triangular numbers using the preceding formula.
Have the program generate every fifth triangular number between 5 and 50 (that
is, 5, 10, 15, ..., 50).

I have no idea how to get it to "generate" every fifth "triangular number between 5 and 50". I put "triangularNumber = n (n + 1) / 2" (I renamed the variables) in my code and it just keeps giving me the following error below, I have no idea what I'm doing wrong, clearly something.

Code:

[james@jamespc devel]$ gcc -Wall -Werror program_looping__exercise3.c -o program_looping__exercise3
program_looping__exercise3.c: In function ‘main’:
program_looping__exercise3.c:14:25: error: called object is not a function or function pointer
plusnumber = number / 2 (number + 1);

I think I might just use software others have already written. I would mark this thread as SOLVED, but given I haven't really learnt how to write a decent program, let alone C programming, I'm not sure I honestly can if you know what I mean. I just don't see it happening. I would post my code, but I'm probably just wasting my time trying to go any further with this. I'm sorry to have wasted everyone's time.

Don't get discouraged, you were actually on the right track.

The exercise states:

Quote:

Write a program to generate and display a table of n and n², for integer values of n ranging from 1 to 10. Be certain to print appropriate column headings.

jpollard misunderstood you -- most likely because the superscript was lost when you cut/pasted it (making it look like a variable named 'n2' when it wasn't). It's just asking you to calculate a table of the squares of n. So yes, as you suspected it's just a matter of doing result = n * n; and outputting it, within a loop.

As for the other formula you were having a problem with, in math they tend to leave out the multiply operators, so when they say something like n(n + 1), that's just a mathematician's way of saying what a programmer would write as n * (n + 1), so whenever you see something like that, you just have to remember to put them back in.

BTW, you may find https://www.mathsisfun.com helpful for filling in some of the gaps in your knowledge, especially the algebra sections.

I can't really say one way or the other what you should do regarding programming but I will suggest that if you would like to further your studies in math, you should check out this site:

I studied math for fun for about 4 years before I committed myself to programming. For the last 2 of those 4 years I used this site. Sal has a friendly, informal approach to teaching. It's like having a conversation with a friend. The majority of his lectures are videos (some reading material) where he talks through the concept but he also illustrates with diagrams, examples, etc. He teaches everything from the very basic to the more advanced. There's also a forum where you can ask questions and they'll be answered by other students, math tutors, etc. It's also free, you just have to sign up for a membership.

Thanks again for your help jpollard. I just don't know what to do. I'm not trying to digress or anything I'm honestly not, and I don't want to waste anyone's time. But I'm clearly no good at maths, I did honestly try, but it's just pissing me off not understanding it. And given what you guys have said about the need to understand it, and I can see why the more I read the C pdf/book, it's starting to look like I just don't have the maths knowledge required for it. And I'm probably gonna be dead by the time I ever can figure it out. So I'm just not sure it's worth trying to continue with it, I do want to learn how to program, but like I said, it's just getting too hard to try and figure the maths out.

I do not believe the term is plural, it would simply be "math", no 's' required.

Regarding the whole topic, all I'm concerned about would be "recognition" that numbers mean certain things, and that you can convert them somehow. The calculator is helpful for that, the part where it converts for you, that is.

What I mean by recognition.

0x30 - what is that? To me, it does mean 48, but it also means "three, sixteens"

Because it is a hex number.

I know that the second digit from the right means "sixteens".

So I know what that number means, by visual inspection.

This is what I mean by recognition.

Go back to base 10. Decimal.

What is the second digit as you move left from the right-most digit?

A: Tens.

If I say, "Show me ninety", you write a nine and then a following zero, because you know that you need nine values of ten.

And you know that this second digit from the right represents tens.

You probably know that intuitively from just "life", however if you think about it, that second digit is "a multiple of ten".

As you head more to the left in digits, you continue to multiply by ten.

Hundreds, thousands, ten thousands, etc. (Show me someone who would not recognize one million. Why? Because that's some expression of money that people would love to see in their pockets.)

So ...

Same thing for another number based system.

Hexadecimal is base 16.

The right most digit is the "ones" digit.

I think you sort of understand that the next digit to the left is the "sixteens" digit.

So as a result if I ask you to represent sixteen in hexadecimal, you end up showing me 0x10. Which is "one value of sixteen".

What's two values of sixteen? 2 x 16 which is 32. How is that in hex? 0x20.

That's really what I wish you to recognize, so that when you are presented with hex numbers, as you very much will be; that you recognize what they mean, and they don't just blow your mind for days on end.

I.e. you can either figure them out, or use your computer calculator to show you the conversion.

That's it. Perhaps get programming, but worry less about the equation of a triangle, or how to calculate the area of a circle. If you're poor at Math, then writing code to compute equations is a bad idea. You can certainly write code to control an I2C device, or GUI code, or database code. No reason to complicate things. And if you understand the math, then writing the code is fine. For instance if you understand how to calculate simple interest, like for a car loan, so you can get the annual interest on some loan, and write a program that calculates your payment for a 5-year car loan, or something like that. If that's too much for you, then it is. Concentrate on some stuff you can do for now.

Don't listen to them. There are people who are programming and those who are teaching programming. So just start to program. Instead of you will be stuck on 'learning programming'. Maybe for months, even year. Just waste of time.

The first few chapters of the Kochan book are a little over focussed on math(s)¹. The example programs have you calculating triangular numbers, gcd, fibonacci sequence and so on. Non of it is particularly hard, but if you're completely new to the topic I can see how it might be a little daunting having to learn all that while trying to pick up the C language and fundamental programming concepts all at once.

¹ Can confirm. Over here in the UK we keep the 's' from mathematics when we abbreviate.

...
That's it. Perhaps get programming, but worry less about the equation of a triangle, or how to calculate the area of a circle. If you're poor at Math, then writing code to compute equations is a bad idea. You can certainly write code to control an I2C device, or GUI code, or database code. No reason to complicate things. And if you understand the math, then writing the code is fine. For instance if you understand how to calculate simple interest, like for a car loan, so you can get the annual interest on some loan, and write a program that calculates your payment for a 5-year car loan, or something like that. If that's too much for you, then it is. Concentrate on some stuff you can do for now.

The problem is that programming is ALL based on math, and programming is MUCH easier if you are familiar with the basics... which is the arithmetic, and then algebra.

You don't get to control an I2C device or GUI without it.

Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.

Distribution: CentOS at the time of this writing, but some others over the years too...

Posts: 2,546

Original Poster

Rep:

Thanks again guys. I appreciate all of you taking the time to respond, as I know it's not as if anyone has to, so thanks again.

I do take all comments as being helpful, being the reason I've marked them all as helpful, even where some of you differ, I still consider it to be helpful to me. Just don't ask me to explain that statement, because I probably won't be able to give an answer that would make a lot of sense. But never-the-less, and while I'm really still in two minds about continuing with it; I would like to clarify a few things to see if I've understood a few things properly.

Thanks for all the help you have given me jpollard. When you say "arithmetic", do you basically mean the usual stuff being, add, subtract, division, multiply ? And when you say "algebra", you mean basically using letters in place of numbers? (I know there's more to it than that, but without repeating everything I've read on Wikipedia, is that the very basic idea?)

Thanks for your answers and help GazL. I tried what you said, and it worked. But I don't think I got the exercise about getting every 5th number right though.

To be clear: I don't intend on becoming an expert in math, and I don't expect to become one. And given where my maths knowledge is, and while it would be nice, probably not doing any paid programming work either. But thanks Mechanikx for that link, while I will have a look at it, it's probably going to take a lot longer for me to dramatically improve my math knowledge to any great degree than what it took you, so I don't have high hopes about it, but thanks for the link all the same.

Anyhow I've posted what I've done below for you guys to have a look at all the same;

Code:

#include <stdio.h>
int main(void)
{
int number, plusnumber = 0;
printf("\nTable of Triangular Numbers\n");
printf("number Sum of 2 to number\n");
printf("------ ------------------\n");
for( number = 1; number <= 10; ++number) {
plusnumber = number * number;
printf("%2i %2i\n", number, plusnumber);
}
return 0;
}

(the code above is for exercise 2. The code below is for exercise 3 - which was supposed to get every 5th number which it doesn't from the output I got do)

Code:

#include <stdio.h>
int main(void)
{
int number, plusnumber = 10;
printf("\nTable of Triangular Numbers\n");
printf("number Sum of 2 to number\n");
printf("------ ------------------\n");
for( number = 0; number <= 10; ++number) {
plusnumber = number * (number + 1) / 2;
printf(" %2i %2i\n", number, plusnumber);
}
return 0;
}

It doesn't matter that you initialize plusnumber to 10 versus 0 since you are assigning it a value in the first place.

Whatever you were supposed to do:

Quote:

get every 5th number

Is an unclear statement.

Meanwhile what you are doing with this:

Code:

plusnumber = number * (number + 1) / 2;

Is that you are taking the loop variable, which has values from 0-10.
Multiplying each iteration of the loop variable times the loop variable plus one, then divided by two:

My guess is that you are trying to make the computation result increase by five each time.

A set of terms to remember:
Please My Dear Aunt Sue
Parenthesis
Multiplication
Division
Addition
Subtraction

This is called PRECEDENCE and it governs the order of operations.

Parenthesis wins over all
Multiplication is above Division
And so forth.

This also goes Left to Right. Thus if you have several of the same operation, (a * b * c * d), then you do them left to right.

I mean, I agree that you need to know Math to program effectively, however you are trying code a numeric algorithm. Bad idea in my humble opinion, you don't seem to grasp numeric algorithms well at all.

Be that it may, what I would recommend would be the following:

Learn how to use GDB and use GDB on all your source files.

I prefer to use GDB through emacs, it provides a great starter interface to give you visual and symbolic debugging. You actually can compile from emacs as well.

Set breakpoints, inspect variables, and single step through your code so that you learn about what it is doing.

A great alternate would be to add a great deal of printf() statements about everything, or including far more details that you have.

Code:

int interim_1, interim_2;
for( number = 0; number <= 10; ++number) {
plusnumber = number * (number + 1) / 2;
interim_1 = number + 1;
interim_2 = interim_1 * number;
printf("Loop variable value: %d\tResultant calculation: %d\nInterim_1: %d\tInterim_2: %d\n", number, plusnumber, interim_1, interim_2);
}

++n is a fancy way of doing n += 1, which is a shorthand way of saying n = n + 1

If you want to go by every 5, just add 5 instead of 1, so instead of ++n use n = n + 5

I think people are over-egging the need for math here. You'll need to understand basic algebra (using letters in place of numbers: because that is essentially what variable expressions are). For C you'll need to understand Binary representation, and Hexadecimal, but the book covers that. C starts getting confusing when you get into pointers and pointer arithmetic and stuff like that (not helped by the fact that the language itself is a little inconsistent) but these are topics for later.

Anyway, rather than give up on programming completely, which would be a shame as it's both a wonderful skill to have and a lot of fun, may I suggest that if you're struggling with C then start with something like python.

When I first started I learnt with BASIC which is a much easier language than C is. I also used to love Turbo Pascal back in those days, but both of those are seldom heard of these days. You can always come back to C in the future as your second language.

Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.

Distribution: CentOS at the time of this writing, but some others over the years too...

Posts: 2,546

Original Poster

Rep:

Thanks Guys.

Thanks for answering that for me GazL. It's not really the C that's hard, yes there are a lot of concepts to cover, but it's the math that's killing me. Even if I learnt Python (not going to rule it out for later on), it would not solve my math (or lack of skills) problems, and I would like to learn C because if I can do that; I'd think learning any other programming language should be a lot easier.

It's all honesty been fairly simple learning C itself, as I did read in that C book about how "++n" is (as you said), the shorthand way of writing "n = n + 1", it just seemed like a good idea to shorten it to me, which was why I used it. It's just the math side of things once again that I'm not really getting.

(and thanks RT for explaining what you did about how the loop works)

I recommend starting a new thread in General where you explain that you're trying to learn Mathematics for programming and describe the problems which you are having.

Until you can intuitively grasp some of the Math concepts, I fear you'll still end up blocked with the exercises you persist with trying in the C course you are following.

A set of terms to remember:
Please My Dear Aunt Sue
Parenthesis
Multiplication
Division
Addition
Subtraction

This is called PRECEDENCE and it governs the order of operations.

Parenthesis wins over all
Multiplication is above Division
And so forth.

Forgive me for interjecting, but this is not true. Multiplication is equal precedence to both division and modulo: all of which are processed left to right.
e.g.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.