# Beginners Tutorial : The C-language.

Discussion in 'Computer Science | IT | Networking' started by simplycoder, Aug 19, 2011.

1. ### varshiva Apprentice

Engineering Discipline:
Computer Science
how can we understand the radius as float

and when will we use const

please explain how to use float,double

#41
2. ### Vishal0203 Ace

Engineering Discipline:
Computer Science
I didn't understand your 1st question..
Coming to the second one, we use const mainly when we are passing an array or a string in the function. Sometimes, there is a possibility that the size of array gets disturbed during passing. so to avoid that we use const key word, which doesn't allow any change in size. i.e. keeps it constant.

We use float and double depending on the situation. like, we know that the percentage of a student can even be in decimals.. Hence, we use float in that situation..

#42
3. ### Jeffrey Samuel Knight

Engineering Discipline:
Electronics
All values entered by the user can never be determined that easily by the programmer in advance that is the reason why we go for float than int in many times

In practical thinking a radius can be both of float value and also an integer so to randomise and to get more accurate value in the op we go for Float instead of int

#43
4. ### varshiva Apprentice

Engineering Discipline:
Computer Science
thank you guys

#44
5. ### Chinu1 Apprentice

Engineering Discipline:
Computer Science
You are doing a great job. Thank you so much. Now, I understand the difference between local and global variables.

#45
6. ### SamiZzY Apprentice

Engineering Discipline:
Electronics & Telecommunications
Code:
```#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{

int a,b,i,c;
printf("Give the number upto which the squares of the numbers are to be added\n\n");
scanf("%d",&a);

c=0;

for(i=0;i<a;i++)
{
b=1+i;
c*c=b*b+c*c; /*help needed in this line*/
}
printf("%d\n",(c*c));

system("PAUSE");
return 0;
}```
first of all thankyou for all the great tutorials simplycoder . And i am kind of stuck in tutorial 11 exercises, the one where you have to find the sum of squares of n numbers. Above is the code i wrote, i am just a newbie its hardly been 3 days since i started C so excuse the noobness please.

#46
7. ### simplycoder Ace

Engineering Discipline:
Electronics & Communications
First of all welcome to CE.
There are few pointers which I like to add.
1)Declaration : Since this is very short program, I dont have to worry much on which variable is used for what purpose. But its slightly tedious when it comes to medium or lengthy programs.
So to avoid this, make sure you use meaningful variable and method names, you would make life easy for person reviewing or understanding you code. Trust me this is very important.
2)Comments : You should and must comment or even better, document your code no matter how small or big the assignment is. This helps a lot and also would be helpful in the future. Comment on the key points and what variable is doing,how it is used,why it is used and so on.
3)Make sections in your code : Sections are always readable and gives a nice impression.
Say you have a program where in you have variable declarations, put them under variable declaration section. Organize your code, I know this is not required in small programs, but doesn't hurt to be more organized, and if you don't do this in lengthy codes, trust me, you are going to end up doing a messy code which would be hard to debug.
4)Write the code in such a way that any person with basic understanding of language can understand your code. as the saying goes and I quote Any fool can write code that a computer can understand. Good programmers write code that humans can understand.- Martin Fowler
5)Solve the problem on piece of paper, write the steps down and also track the program for small input, if this is done, then only try to code in programming language (VVIMP).

Now coming back to your problem.
a is used for totalNumbers,
b is where you assign the current number,
c is the output variable.

I understood this line b=1+i; as i starts from 0 which is the index of for loop, so b=i+1 so when i=0, b=1 and so on. I understood this as the current number whose square you are calculating.
However, I am not able to understand this line
c*c=b*b+c*c; /*help needed in this line*/
Why have you squared the left hand side? The C-compiler will definitely throw an error on compilation, and it should be something like 'Invalid lvalue assignment'.
I am even more confused when you used printf("%d\n",(c*c));
I suggest you should focus more on algorithmic aspect of the problem. Donot worry much about C-Language, any one can program in C language with practice of about a month or two(I am talking about just writing and not being expert in C). And programming a problem is simpler as compared solving and developing an algorithm for the problem as you can see, you can program in C, but its the algorithm and the approach that has led you to trouble.(Donot misunderstand me, I am writing this much not to brag about me, but rather I recalled the days when I began to program, it was worse than this and had to undergo a lot of trouble to make things work, so trying to make your life easier. You would be going through some hard stuff, just dont give up in case of any doubt post, on a new thread,
On this forum, we share, no one is noob, we share and gain knowledge that is one of the best things on this forum so feel free to post.
Anyways here is what I have coded for you, ask if you are stuck any where.

PHP:
``` //Author: SimplyCoder.//Date: 06/12/2012.// IDE : Dev C++/********************************************************************************/                            //Problem Statement.(Tutorial 11, Problem 4)/********************************************************************************//*****WAP to find the sum of series 1+4+9+16+...n^2 where the input is take from *//*****user, using for,while and do...while looping                              *//*****(DONOT use formula, however verify using the formula).                    *//********************************************************************************/ //Macros Declaration-Start.#include<stdio.h>//Macros Declaration-End. //Function-Declaration-Start.    int findSum(int totalNumbers);//Function-Declaration-End.//Main Method-Start.    int main(int argc,char**argv)        {        //Declaration for local variables-Start.            int totalNumbers=0; // total numbers in the series : 1+4+9...+n^2, correponds to n.            int finalAnswer=0; // final answer : Sum of the given series.        //Declaration for local variables-End.           //Take the User Input-Start.            printf("Give the number upto which the squares of the numbers are to be added :: ");            scanf("%d",&totalNumbers);        //Take the user Input-End.               finalAnswer=findSum(totalNumbers);//Calculate the output.            printf("\nThe sum is :: %d\n",finalAnswer);//print the answer.               printf("\n\nExecution Complete, System Paused.\n");            system("pause");        }//Main Method-End. //Local Methods-Start. //findSum Method-Start./***************************************************************************///Accepts an integer.//Returns an integer.//Used to calculate the sum of the series./***************************************************************************/     int findSum(int totalNumbers)        {        //Variable Declaration-Start.            int index=0; //index for the 'for-loop'            int finalAnswer=0; // final answer : Sum of the given series.        //Variable Declaration-End.                   //for loop below calculates the sum for the required            for(index=1;index<=totalNumbers;index++)                {                    finalAnswer=finalAnswer+(index*index);                  }            return finalAnswer;        }//findSum Method-End.  //Local-Method-End. ```
Take care,
SimplyCoder.

• Like x 2
• Informative x 1
#47
8. ### SamiZzY Apprentice

Engineering Discipline:
Electronics & Telecommunications
Wasn't expecting a reply but this is amazing ,nice people still exist. Thankyou very much for the reply.
Regarding the line ,c*c=b*b+c*c.... i didnt know about the findsum function , so i started with a code for the ' find the sum of n numbers' which is

final_ans=0
for(index=0,index<total_number,index++)
{ current_number=1+index;
final_ans=final_ans+index;
}
printf("%d",final_ans);

so i figured if i wanted to find the sum of squares of the numbers , i just have to square the final equation terms i.e,( final_ans*final_ans)=(final_ans*final_ans)+(current_number*current_number);
which turns out is absolutely wrong and i get why, thanks to you.And also thankyou for valuable tips will make sure to follow them.

#48
9. ### SamiZzY Apprentice

Engineering Discipline:
Electronics & Telecommunications
when i execute the 'find sum of n number' program, it gives an error "undefined reference to' findsum' ".
some prob with my compiler or something ?

#49
10. ### simplycoder Ace

Engineering Discipline:
Electronics & Communications
Thanks for the compliment.

Which compiler are you using?
I am using Dev C++ as my IDE and its default compiler.

Please donot use Turbo C (One with Blue Screen).

#50
11. ### SamiZzY Apprentice

Engineering Discipline:
Electronics & Telecommunications
yes I am also using Dev C++ v 4.9.9.2, downloaded it from the link provided in the 1st page.

#51
12. ### simplycoder Ace

Engineering Discipline:
Electronics & Communications
In that case, kindly verify the it with my code, if you have copy pasted it, copy paste it again and let us know.

#52
13. ### rahul69 Star

Engineering Discipline:
IT
SamiZzY
and
From above quotes I guessed that u are probably considering findsum as pre-existing like Library functions, which is not true, this findsum function is a user defined function and hence u will have to also write the function definition (ie function code) in your program. This "undefined reference" error also comes when u use a function without including its definition, and only declaration. Hope it helps!!!

• Like x 1
#53
14. ### SamiZzY Apprentice

Engineering Discipline:
Electronics & Telecommunications
Code:
```#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{int total_number,index,final_ans;

printf("Give the number upto which the square of the numbers are to be added::\n");

scanf("%d",&total_number);

final_ans=0;

for(index=1;index<=total_number;index++)
{
final_ans=final_ans+(index*index);
}
printf("The Sum of the squares of %d numbers is %d",total_number,final_ans);
system("PAUSE");
return 0;
}```
the code you gave me , it worked but now i am even more confused.
Above is the code which i wrote after observing yours(for finding sum of squares of n num and it works XD).
the reason why it was giving error was because i copy pasted the code only uptill Main Method-End.
I thought above and below main method-end in your program are two different codes , one for the sum of n numbers and the other for the sum of the squares of n numbers.
confused confused confused.!

#54
15. ### Jeffrey Samuel Knight

Engineering Discipline:
Electronics
That main method end was in comment and SC used Function call in his program The findsum function was called in the main program.

They are different functions But they are same programs

PS: // any line beginning with this '//' is a comment line and they are not considered by the compiler They are given for your understanding and does not mark the end of the program

#55
16. ### SamiZzY Apprentice

Engineering Discipline:
Electronics & Telecommunications
yeah i was beginning to think like that thanks for clarification. See the program worked now because i copied the whole code. so maybe it is defined somewhere ? i cant find it lol help

#56
17. ### SamiZzY Apprentice

Engineering Discipline:
Electronics & Telecommunications
yes i knw that they are comments, i just used it so as to say below or above it.

#57
18. ### simplycoder Ace

Engineering Discipline:
Electronics & Communications
Check in function declaration section.

#58
19. ### SamiZzY Apprentice

Engineering Discipline:
Electronics & Telecommunications
sir, ... it seems i am stuck again but this time for sure, i know the logic.
Below is the code for verifying an armstrong number(tut: 13 exercise 3) and thankfully it works.

Code:
```#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])

{int input,index,final_ans,index2,n,temp,digit,original_input;
printf("Give the number to be verified if it's an armstrong number or not ::\n");
scanf("%d",&input);
final_ans = 0;
index=0;
n=0;
//FINDING THE NUMBER OF DIGITS-START.
while(temp!=0)          //when temp reaches 0 stop the loop
{index=index+1;
temp=input/pow(10,index);
n++;                    // n is initially 0 , placed n here as there will be an increment in n by +1 with each loop

}
//FINDING THE NUMBER OF DIGITS-END.
original_input=input; // storing the input in another int as our integer will be changing in the main code

//FINDING THE FINAL ANSWER PART.(MAIN CODE)
for(index2=1;index2<=(n);index2++)
{
digit = input%10;          //we get the unit place digit for the input
final_ans = final_ans + pow(digit,n); //the digit is raised to nth power and added to other nth power digits.
input=input/10;                      //since the input's last digit is found, in order to remove last digit we divide by 10.

}
if(final_ans==original_input)
printf("The given number %d is a Armstrong number\n",original_input);
else
printf("The given number is not a Armstrong number\n");

system("PAUSE");
return 0;
}```
so one may think that, if verifying a number is possible then printing armstrong number is no biggie.
I thought so too. i wrote the code, and i have no idea why it just prints 1 infinitely.
Code:
```#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{    int index,index2,final_ans,original_index,digit;

for(index=100;index<=999;index++)
{
original_index=index;
final_ans=0;

for(index2=1;index2<=3;index2++)
{
digit=index%10;
final_ans=final_ans+pow(digit,3);
index=index/10;
}

if(final_ans==original_index)
printf("%d\n",final_ans);
}

system("PAUSE");
return 0;
}
```
Above is the code for printing armstrong number from 100 to 999(tut 13 exercise 4).
It would be very helpful if you could correct me. Thankyou in advance.

#59
20. ### Jeffrey Samuel Knight

Engineering Discipline:
Electronics
Code:
```for(index2=1;index2<=3;index2++)
{
digit=index%10;
final_ans=final_ans+pow(digit,3);
index=index/10;
}```
instead of the variable index use original_index here that should solve the problem here

#60