# New to programing!



## Hassan786 (Jan 6, 2012)

Hi there!
I got admission in MCS (masters in computer sciences), where I've one subject "introduction to programing".. It's about C and C++..
I just got familiar with C's basic commands...
My this post is for seeking any kind of help which I may need during my course.... So I'll be v.v.v.v.very thankful to all of ya who will provide me any kinda help...
First of all tell me that which is the best software to write a program in C?/...


and the second thing that I'm currently using "DEV C++" for editing purpose... but when I run my program the DOS crashes and I'm not able to find the result of a single program which I've created... 

any solution??????????????

thanx and PEACE:

Hassan Ziaopcorn:


----------



## Special2God (Oct 5, 2009)

Congratulations on your admission to MCS. 
I suggest using netbeans.org/features/cpp/.
I originally started learning C in DEV C++ and did not like it, I prefer netbeans, since it has better code completion, and more support and plugin options. 

I don't know why your programs might be crashing, it might be DEV C++, it might be a bug in the code, it might be something else. 
Feel free to post code that you might be having trouble with and myself or someone in the programming forum can help you.


----------



## Hassan786 (Jan 6, 2012)

Thankyou for your reply;
I'm now going to download Netbeans.
I've edited my "registry" many times so maybe it is cause for crashes...
Can it be fixed by installing a fresh copy of windows?


----------



## Babbzzz (Jul 14, 2011)

Yes. It's never a bad idea to start afresh. It's the easiest way.


----------



## Hassan786 (Jan 6, 2012)

I made this program through DEV C++,,, But compiler does not compile it.... Let me know that where I'm wrong?????

#include<iostream.h>
main()
{
//declaration of variables, the age will be in whole numbers
int age1,age2,age3,age4,age5,age6,age7,age8,age9,age10;
int totalage,averageage;
//take ages of the students from the user
cout<<"please enter the age of student 1:";
cin>>age1;
cout<<"please enter the age of student 2:";
cin>>age2;
cout<<"please enter the age of student 3:";
cin>>age3;
cout<<"please enter the age of student 4:";
cin>>age4;
cout<<"please enter the age of student 5:";
cin>>age5;
cout<<"please enter the age of student 6:";
cin>>age6;
cout<<"please enter the age of student 7:";
cin>>age7;
cout<<"please enter the age of student 8:";
cin>>age8;
cout<<"please enter the age of student 9:";
cin>>age9;
cout<<"please enter the age of student 10:";
cin>>age10;

//calculate the total age and average age
totalage=age1+age2+age3+age4+age5+age6+age7+age8+age9+age10;
cout<<"Total Age of class:"<<totalage
averageage=totalage/10;

//display the result for average age
cout<<"Average Age of the class:"<<averageage;
}


----------



## Special2God (Oct 5, 2009)

cout and cin are C++ functions that can must use the :: operator, similar to the . operator in Java, C#, PHP, etc. 
This code worked for me once the necessary changes had been made. 

```
#include <iostream>

int main() {
	//declaration of variables, the age will be in whole numbers
	int age1,age2,age3,age4,age5,age6,age7,age8,age9,age10;
	int totalage,averageage;
	//take ages of the students from the user
	std::cout<<"please enter the age of student 1:";
	std::cin>>age1;
	std::cout<<"please enter the age of student 2:";
	std::cin>>age2;
	std::cout<<"please enter the age of student 3:";
	std::cin>>age3;
	std::cout<<"please enter the age of student 4:";
	std::cin>>age4;
	std::cout<<"please enter the age of student 5:";
	std::cin>>age5;
	std::cout<<"please enter the age of student 6:";
	std::cin>>age6;
	std::cout<<"please enter the age of student 7:";
	std::cin>>age7;
	std::cout<<"please enter the age of student 8:";
	std::cin>>age8;
	std::cout<<"please enter the age of student 9:";
	std::cin>>age9;
	std::cout<<"please enter the age of student 10:";
	std::cin>>age10;

	//calculate the total age and average age
	totalage=age1+age2+age3+age4+age5+age6+age7+age8+age9+age10;
	std::cout<<"Total Age of class:"<<totalage;
	averageage=totalage/10;

	//display the result for average age
	std::cout<<"Average Age of the class:"<<averageage;
	return 0;
}
```
Hopefully that code works, I simply added *std::* to the beginning of *cout* and *cin* function calls and also added a semicolon to one of the lines that was missing it. I also changed the #include statement from <iostream.h> to <iostream> since common libraries such as iostream are well known by the compiler and do not need the .h extension.


----------



## Hassan786 (Jan 6, 2012)

*Automata Problem....*

I'm just a beginner of AUTOMATA study.....

will some one please help me in this string????????????????????


*"* Σ = {a, b}

*Write a Regular Expression*, that only accepts strings with exactly two or three a’s in the string. There is no restriction on occurrence of b’s in the string.

Few examples of accepted strings are as under;
- bbaabbbba
- baba
- aba
- abaa
- aa
- aaa
etc.








please reply as soon as possible


----------



## Special2God (Oct 5, 2009)

*Re: Automata Problem....*

The language is important, regular expressions vary in different programming languages, please explain your situation and the programming language you are using. 

Also, you placed this: [" Σ = {a, b}] expression in your post without explaining what its purpose is. Is it related to the problem you are trying to solve?


----------



## Hassan786 (Jan 6, 2012)

*Re: Automata Problem....*

It's just beginning of this subject and I don't know which language it is....
I was just asked to make a RE according to above condition..
However I made some solution and my RE is this:

b*ab*ab*+b*ab*ab*ab*


is this a correct string?

And I'm very very thankful to you for helping me...
I need your help very much as I'm very new to programming.
Thanks again


----------



## Hassan786 (Jan 6, 2012)

I'm very thankful for giving me solution...
But problem is that book which is recommended for our MCS 1st semester is "Dietle & Dietle How to program"...

There I didn't read about "std::" statement yet.. So I'm confused.. and all sample programs of first 5 chapters are using <iostream.h> instead of <iostream>...
what's difference between these?
Thankyou. 

You program was executed until i entered the age of 10th student... the program crashed... why?
and 

I my self made program works for addition,,, but when I enter the variable for "averageage" compiler shows error.


----------



## joeten (Dec 4, 2008)

Threads merged


----------



## Special2God (Oct 5, 2009)

I'm not sure why it might be crashing, try commenting out one or two lines of code using "//" at a time to figure out which line of code is causing the crash. 

std::cout and std::cin are fuctions from the iostream library and are part of C++, printf and scanf are from stdio.h and are part of C, most compilers don't recognize iostream.h because it is not a valid library name. 

You can 'include' std:: in your program so that you don't have to type it before commands by adding the highlighted line of code to your program. This will allow you to remove 'std::' before each cout and cin function call. 

```
#include <iostream>
[B]using namespace std;[/B]

int main() {
//declaration of variables, the age will be in whole numbers
	int age1,age2,age3,age4,age5,age6,age7,age8,age9,age10;
	int totalage,averageage;
	//take ages of the students from the user
	cout<<"please enter the age of student 1:";
	cin>>age1;
	cout<<"please enter the age of student 2:";
	cin>>age2;
	cout<<"please enter the age of student 3:";
	cin>>age3;
	cout<<"please enter the age of student 4:";
	cin>>age4;
	cout<<"please enter the age of student 5:";
	cin>>age5;
	cout<<"please enter the age of student 6:";
	cin>>age6;
	cout<<"please enter the age of student 7:";
	cin>>age7;
	cout<<"please enter the age of student 8:";
	cin>>age8;
	cout<<"please enter the age of student 9:";
	cin>>age9;
	cout<<"please enter the age of student 10:";
	cin>>age10;

	//calculate the total age and average age
	totalage=age1+age2+age3+age4+age5+age6+age7+age8+age9+age10;
	cout<<"Total Age of class:"<<totalage;
	averageage=totalage/10;

	//display the result for average age
	cout<<"Average Age of the class:"<<averageage;
	return 0;
}
```


----------



## AlbertMC2 (Jul 15, 2010)

Your Regular Expression is wrong.
because you can have 2 or 3 "a" you will have to use brackets ([]) and an or (?)

Because you are learning automata I presume you are also learning finite and deterministic finite automata. I found that it was always easier to draw the automata, then use the state removal method to convert it to an RE.

Other sites that you can look at are:
To test an RE on example data: Regex Tester
RE reference: Regular Expressions Reference - Basic Syntax

Note Regular expressions are not language specific. They are used in most programming languages, translators and compilers (among other uses) to recognize patterns and words. Admittedly all programming languages have their small differences when dealing with regular expressions but the basics are always the same. the fact that you are learning automata shows that you are learning the basic idea of an RE and how to recognize a language, word or pattern given a language {a, b}.


----------



## Hassan786 (Jan 6, 2012)

Special2God said:


> I'm not sure why it might be crashing, try commenting out one or two lines of code using "//" at a time to figure out which line of code is causing the crash.
> 
> std::cout and std::cin are fuctions from the iostream library and are part of C++, printf and scanf are from stdio.h and are part of C, most compilers don't recognize iostream.h because it is not a valid library name.
> 
> ...




I'm very thankful for giving me solution...
But problem is that book which is recommended for our MCS 1st semester is "Dietle & Dietle How to program"...

There I didn't read about "std::" statement yet.. So I'm confused.. and all sample programs of first 5 chapters are using <iostream.h> instead of <iostream>...
what's difference between these?
Thankyou. 

You program was executed until i entered the age of 10th student... the program crashed... why?
and 

I my self made program works for addition,,, but when I enter the variable for "averageage" compiler shows error.


----------



## AlbertMC2 (Jul 15, 2010)

You declared average age as an int. Perhaps when dividing totalage by 10 it does not give an int but a float?


----------



## Hassan786 (Jan 6, 2012)

*I am unable to understand that what logic should I use.... C problem...*

*Problem Statement: *
Write the C++ language program to calculate the slope of the line that is passing through two points and displays one of the following five messages according to the nature of the slope.

Message No
Description
1​ *Line will make the 45 degree angle with the horizon*
2​ *Line will travel more along X-Axis and Less along Y-Axis*
3​ *Line will travel more along Y-Axis and Less along X-Axis*
4​ *Line is parallel to Y-Axis*.
5​ *Line is parallel to X-Axis*.​ 
*Detailed Description:*

Your program will ask for the two inputs for the starting point P1(x1,y1) of the line i.e. X-Coordinate and 
Y-Coordinate, and two inputs for the ending point P2(x2,y2) of the line i.e. X-Coordinate and Y-Coordinate. 

Formula to calculate the slope of the line is

Slope = difference between y coordinates/difference between x coordinates









Slope can be one of the following five types depending upon its value.



Slope=1
 *Line will make the 45 degree angle with the horizon*



Slope<1
 *Line will travel more along X-Axis and Less along Y-Axis*



Slope>1
 *Line will travel more along Y-Axis and Less along X-Axis *



Your program should have a check for the value of difference between X coordinates of the two points i.e. dx. In case difference between x coordinates is zero then slope should not be calculated in your program and following message should be displayed.
 
* Line is parallel to Y-Axis*.



Your program should have a check for the value of difference between Y coordinates of the two points i.e. dy. In case if the difference between y coordinates is zero then following message should be displayed.
 
* Line is parallel to X-Axis*.

*For example *Consider a line which passes through two points p1(10, 10) and p2(20, 18).
Your program should ask for X-coordinate of the starting point i.e. 10
Then it should prompt for the Y-coordinate of the starting point i.e. 10
Similarly your program will ask for the X and Y-coordinates of the ending point of the line i.e. 20 and 18 respectively.
Now difference between y coordinates is dy =







= 18 – 10 = 8
Whereas difference between x coordinates is dx =







= 20 – 10 = 10









Then message should be displayed according to the nature of the slope of the line. In this case following message will be displayed.


----------



## AlbertMC2 (Jul 15, 2010)

*Re: I am unable to understand that what logic should I use.... C problem...*

Hi

We are not allowed to help with homework. But I will try point you in the correct direction.
Firstly, the example is pretty straight forward. It shows you exactly the logic that should be used.

In pseudo language this is how your program will work:

```
enter start points
enter end points
check x coords
   if equal display message and exit
check y coords
   if equal display message and exit
else
   calculate slope of line
   if slope=1 display message
   if slope>1 display message
   if slope<1 display message
exit
```


----------



## Hassan786 (Jan 6, 2012)

*Re: I am unable to understand that what logic should I use.... C problem...*

Actually this is not homework but a problem of my own interest...
and I need to code in C


----------



## niemiro (Feb 20, 2010)

*Re: I am unable to understand that what logic should I use.... C problem...*



Hassan786 said:


> Actually this is not homework but a problem of my own interest...
> and I need to code in C


Well, regardless, AlbertMC2 has given you the answer above...

And the idea is for you to convert it into C. Psuedo-code isn't in any particular language, it is just planning out the program structure in plain English.


----------



## joeten (Dec 4, 2008)

*Re: I am unable to understand that what logic should I use.... C problem...*

Hassan will you stop making threads on the same issue of c++


----------



## joeten (Dec 4, 2008)

Threads merged please use only this thread for this issue New to programing!


----------



## Hassan786 (Jan 6, 2012)

Ok... Joeten I'm sorry If I went against forum's rules...
Actually this is the only one help site which I use and trust...
So I post my every problem relating computer here...


----------



## joeten (Dec 4, 2008)

Hi that's great and I'am glad your happy with the help you receive but please only make one thread on any issue unless advised to do otherwise ie in the case of virus,trojan or spyware help.


----------



## Hassan786 (Jan 6, 2012)

plz correct me in this...









#include <allegro.h>

BITMAP *xSprite;
BITMAP *oSprite;

int board[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0}; //This will be used to keep
//track of the Xs and Os
int curSquare = 0; //This will keep track of the current square
//the selector is on

int turn = 1; //This will keep track of whose turn it is
//1 Will be for X and 2 for O

int x = 0; //X and Y position of selector
int y = 0;

int tempX = 0; //holds temporary values used to clear selector
int tempY = 0;

void setupBoard(){ //This function will draw in the grid

acquire_screen();

line( screen, 200, 0, 200, 480, makecol( 255, 255, 255));
line( screen, 400, 0, 400, 480, makecol( 255, 255, 255));
line( screen, 0, 150, 680, 150, makecol( 255, 255, 255));
line( screen, 0, 300, 680, 300, makecol( 255, 255, 255));

rect( screen, x+1, y+1, x + 199, y + 149, makecol( 255, 255, 0));

release_screen();

} 

void updateBoard(){ //draws in selector

rect( screen, tempX+1, tempY+1, tempX + 199, tempY + 149, makecol( 0, 0, 0)); 
rect( screen, x+1, y+1, x + 199, y + 149, makecol( 255, 255, 0)); 
rest(100);
} 

void announceWinner(){ //Announces the winner


if( turn == 1){
textout_ex( screen, font, "X Wins!!!!", 300, 240, makecol( 255, 0, 0), makecol(0, 0, 0)); 
} else {
textout_ex( screen, font, "O Wins!!!!", 300, 240, makecol( 255, 0, 0), makecol(0, 0, 0)); 
} 


}

void checkWin(){ //checks for a winner

if( board[0] == turn && board[1] == turn && board[2] == turn){
announceWinner();
} else if( board[0] == turn && board[3] == turn && board[6] == turn){
announceWinner();
} else if( board[0] == turn && board[4] == turn && board[8] == turn){
announceWinner();
} else if( board[1] == turn && board[4] == turn && board[7] == turn){
announceWinner();
} else if( board[2] == turn && board[4] == turn && board[6] == turn){
announceWinner();
} else if( board[2] == turn && board[5] == turn && board[8] == turn){
announceWinner();
} else if( board[3] == turn && board[4] == turn && board[5] == turn){
announceWinner();
} else if( board[6] == turn && board[7] == turn && board[8] == turn){
announceWinner();
}

} 



void drawXO(){ //draws in the X and O

acquire_screen();

if(turn == 1){
draw_sprite( screen, xSprite, x, y);
board[curSquare] = 1;
checkWin();
++turn; 
} else if( turn == 2){
draw_sprite( screen, oSprite, x, y);
board[curSquare] = 2;
checkWin();
--turn;
}

release_screen();

rest(100);

} 

void moveBox(){ //takes input

clear_keybuf();
tempX = x;
tempY = y;

if( key[KEY_UP] && y != 0){

y -= 150;
curSquare -=3;
updateBoard();

} else if( key[KEY_DOWN] && y != 300){

y += 150;
curSquare +=3;
updateBoard();

} else if( key[KEY_RIGHT] && x != 400){

x += 200;
++curSquare;
updateBoard();

} else if( key[KEY_LEFT] && x != 0){

x -= 200;
--curSquare;
updateBoard();

} else if( key[KEY_ENTER] && board[curSquare] == 0){

drawXO();

} 

} 

int main(){

allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);

xSprite = load_bitmap( "x.bmp", NULL);

oSprite = load_bitmap( "o.bmp", NULL);

setupBoard();

while( !key[KEY_ESC]){
moveBox(); 

} 

destroy_bitmap( xSprite);
destroy_bitmap( oSprite);

return 0;

} 
END_OF_MAIN();


----------

