PDA

View Full Version : Post ur C/C++ Programs Here


Pages : 1 [2]

Sykora
29-09-2007, 08:23 AM
GIMP definitely uses GTK2+ Environment. If I remember correctly, you are required to install it before installing the GIMP itself.

shady_inc
29-09-2007, 09:45 AM
@ Sykora: You have maintained a excellent blog,buddy.:p

Zeeshan Quireshi
29-09-2007, 11:18 AM
^^thanks for confirming.Dev c++ has very good gui too. dud then u seriously need to use Visual C++ Express or Eclipse . You'll forget DevC++ after using them .

Download Visual C++ 2005 Express Here [FREE]:
http://msdn2.microsoft.com/hi-in/express/aa700735.aspx

or you can down Eclipse C++ Developer Pack Here [FREE}:
http://www.eclipse.org/downloads/

The only problem with Eclipse is , t does not Bundle a Compiler with it . so you need to Install GCC(MinGW or Cygwin or DJGPP on Windows) before installing it and then configure it to use ur Installation of GCC , whereas Visual C++ 2005 Express bundles everything into a neat package .

aditya.shevade
29-09-2007, 11:49 AM
GTK is surely available on windows. There's gaim/pidgin and gimp both available for windows. The former surely makes use of GTK on Windows, not sure of the latter.

hmm... I think I should do a little digging on it and see if I can program my project for this year in GTK and port it to windows..... Hoping for the best.

ayush_chh
29-09-2007, 01:25 PM
pls help me out....and explain the cause of my warning.

Sykora
29-09-2007, 01:26 PM
@shady_inc : Why thank you. :)

@ ayush_chh :
From what I've read of the insnode() function, the code is a mess.

For example, you're mallocing a block of memory at temp->link, without ever initializing temp.

The second thing is, the statement you've bolded, ie n1->link;, isn't supposed to do anything by itself. Perhaps you meant n1->link = temp or something?

EDIT : On further study, I think what you meant was :


if(desti==1)
{
temp=(node *)malloc(sizeof(node));
printf("\nEnter the item to be inserted\n");
scanf("%d",&temp->info);
temp->link=n1;
n=temp;
/*n1->link;*/
printf("\nNode Inserted Successfully\n");
printf("\nThe new Linked List elements are\n");
break;
}

quan chi
29-09-2007, 02:46 PM
well how to get the 40th fibonacci numer.
the below only calculates upto 20 i think.

#include<iostream.h>
int fib(int n);
int main
{int n,answer;
cout<<"enter a number"<<endl;
cin>>n;
answer=fib(n);
cout<<answer<<"is the"<<n<<"th fibonacci number"<,endl;
return(0);
}
int fib(int n)
{if(n<3)
{return(1);
}
else
{return(fib(n-2)+fib(n-1));
}
}

Sykora
29-09-2007, 04:07 PM
^^^ Are you asking how to calculate up to the 40th, because you're unable to reach past 20? If that's the case, declare your variables as unsigned long, and then try it.

Zeeshan Quireshi
29-09-2007, 08:13 PM
@quan_chi , it would me much more efficient(actually bout Infinitely Efficient) to solve the program iteratively rather than recursively .

Also u can use BNU Multi Precision library if you want to work with Infinite(literally) precision .
http://gmplib.org/

this way u can calculate as big as ur RAM allows to with no size restraints :)

quan chi
29-09-2007, 09:43 PM
If that's the case, declare your variables as unsigned long, and then try it.
by that it dosent show any answer it freezes for some time and returns to the program page.

@quan_chi , it would me much more efficient(actually bout Infinitely Efficient) to solve the program iteratively rather than recursively .

Also u can use BNU Multi Precision library if you want to work with Infinite(literally) precision .
http://gmplib.org/

this way u can calculate as big as ur RAM allows to with no size restraints :)

well can you please tell me in detail what it is and how to use it .

aditya.shevade
30-09-2007, 01:31 AM
^^^ Are you asking how to calculate up to the 40th, because you're unable to reach past 20? If that's the case, declare your variables as unsigned long, and then try it.

I don't think that the 40th number might be above integer range.

Gigacore
30-09-2007, 06:47 AM
[B] A C Program to convert decimal number to its Binary Number Equivalent


#include <stdio.h>
#include<conio.h>
void main()
{
auto int dec, bin;
int dec_to_bin (int);
printf("Enter a decimal number \n");
scanf("%d", &dec);
bin = dec_to_bin (dec);
printf("The decimal number is = %d \n", dec);
printf("The binary number is = %d \n", bin);
}
/* Funtion to find the binary equivalent */
int dec_to_bin (int d)
{
auto int b,r,k;
b=0;
k=0;
while(d>0)
{
r = d % 2;
d = d / 2;
b = b + r * k;
k = k * 10;
}
return (b);
getch();
}

Sykora
30-09-2007, 08:13 AM
You're better off writing the output binary number to a string rather than an int with digits 0 and 1. That way you'll be able to handle numbers above 31. Right now, the maximum binary number you can store is 11111, which is 31. Above that, and you'll get a wrap around.

The_Devil_Himself
30-09-2007, 08:15 AM
^thats true but what if we are required to first covert 2 decimal no. into binary and then do some arithmatic calculation and then convert the result back to decimal?

Sykora
30-09-2007, 08:25 AM
If you're that into Binary operations, you ought to be writing a class for binary numbers and overloading their operators. If you go that way, there's all sorts of cool things you can do. But that's in C++/Java/Python/Some other OO language.

If you want to stick with C, it is possible to write arithmetic functions over strings, but its slightly cumbersome.

QwertyManiac
30-09-2007, 08:35 AM
I don't think that the 40th number might be above integer range.
Yes its odd, an int actually displays numbers even upto the 47th Fibonacci value :?

A sample output from the program below:
38: 24157817
39: 39088169
40: 63245986
41: 102334155
42: 165580141
43: 267914296
44: 433494437
45: 701408733
46: 1134903170
47: 1836311903
48: -1323752223 (Goes wrong here onwards)

All this through an Integer variable whose max size is supposed to be 32768?! Or is it some kind of a clever compiler behavior, etc?

Here's the code which did the above:
#include<iostream>

using namespace std;

int main()
{
int prev=0, fib=0, final=1;
cout<<"1: "<<prev<<endl<<"2: "<<final<<endl;
for(int i=0;i<=97;i++)
{
fib=final+prev;
prev = final;
final = fib;
cout<<i+3<<": "<<fib<<endl;
}
cout<<endl<<"Final: "<<fib<<endl;
return 0;
}

Doing the same with float yeilds right values upto 100, which would be the obvious way to do so correctly:
#include<iostream>

using namespace std;

int main()
{
float prev=0, fib=0, final=1;
cout.precision(30);
cout<<"1: "<<prev<<endl<<"2: "<<final<<endl;
for(int i=0;i<=97;i++)
{
fib=final+prev;
prev = final;
final = fib;
cout<<i+3<<": "<<fib<<endl;
}
cout<<endl<<"Final: "<<fib<<endl;
return 0;
}

Oddly, using long int results in the same execution as int itself, with numbers crapping out at 48th value and above.

Sykora
30-09-2007, 08:45 AM
Qwerty, compile and run this code, and tell me what you get.


#include <iostream>
#include <limits>

using namespace std;

int main () {
cout << "Data Type\t\tSize\t\tMinimum\t\t\tMaximum\n";
cout << "char\t\t\t" << sizeof(char) << "\t\t" << CHAR_MIN << "\t\t\t" << CHAR_MAX << endl;
cout << "short int\t\t" << sizeof(short int) << "\t\t" << SHRT_MIN << "\t\t\t" << SHRT_MAX << endl;
cout << "int\t\t\t" << sizeof(int) << "\t\t" << INT_MIN << "\t\t" << INT_MAX << endl;
cout << "long int\t\t" << sizeof(long int) << "\t\t" << LONG_MIN << "\t\t" << LONG_MAX << endl;
cout << "unsigned char\t\t" << sizeof(unsigned char) << "\t\t" << '0' << "\t\t\t" << UCHAR_MAX << endl;
cout << "unsigned short\t\t" << sizeof(unsigned short) << "\t\t" << '0' << "\t\t\t" << USHRT_MAX << endl;
cout << "unsigned int\t\t" << sizeof(unsigned int) << "\t\t" << '0' << "\t\t\t" << UINT_MAX << endl;
cout << "unsigned long\t\t" << sizeof(unsigned long) << "\t\t" << '0' << "\t\t\t" << ULONG_MAX << endl;
return 0;
}


You're right, I'm getting the same values for INT_MAX and LONG_MAX. 32767 is for SHRT_MAX.

The_Devil_Himself
30-09-2007, 08:52 AM
got this:
Data Type Size Minimum Maximum
char 1 -128 127
short int 2 -32768 32767
int 4 -2147483648 2147483647
long int 4 -2147483648 2147483647
unsigned char 1 0 255
unsigned short 2 0 65535
unsigned int 4 0 4294967295
unsigned long 4 0 4294967295

QwertyManiac
30-09-2007, 08:54 AM
Am getting the same:
Data Type Size Minimum Maximum
char 1 -128 127
short int 2 -32768 32767
int 4 -2147483648 2147483647
long int 4 -2147483648 2147483647
unsigned char 1 0 255
unsigned short 2 0 65535
unsigned int 4 0 4294967295
unsigned long 4 0 4294967295

So my educational system is at fault? :p

Sykora
30-09-2007, 09:23 AM
So it would seem. We'll only be able to tell if you run the same thing on TC++.

QwertyManiac
30-09-2007, 12:24 PM
I don't have that but here's anomit's (http://oni.ifastnet.com) screen-shot of it running in TC++:

http://img216.imageshack.us/img216/7272/opuk5.jpg

So TC++'s int is short int, guess that solves the issue.

Zeeshan Quireshi
30-09-2007, 12:25 PM
So it would seem. We'll only be able to tell if you run the same thing on TC++.
Well TC++ Generates 16 Bit Executables , so it's Int n Floats would be Half the size of a Present Day 32 Bit Compiler , so in short this code should not work in TC++ :)

So TC++'s int is short int, guess that solves the issue. Yups , Int on a 16 Bit System is Equal to Short on a 32 Bit System .

QwertyManiac
30-09-2007, 12:30 PM
Yeah got that :)

Desi-Tek.com
30-09-2007, 01:15 PM
yea man dev c++ looks more geeky(hence cool).Lols.dev c++ is light on resources and thats very important for a IDE.
dud then u seriously need to use Visual C++ Express or Eclipse . You'll forget DevC++ after using them .

Download Visual C++ 2005 Express Here [FREE]:
http://msdn2.microsoft.com/hi-in/express/aa700735.aspx

or you can down Eclipse C++ Developer Pack Here [FREE}:
http://www.eclipse.org/downloads/

The only problem with Eclipse is , t does not Bundle a Compiler with it . so you need to Install GCC(MinGW or Cygwin or DJGPP on Windows) before installing it and then configure it to use ur Installation of GCC , whereas Visual C++ 2005 Express bundles everything into a neat package .
or try netbeans
http://www.netbeans.org/images/v5/product-pages/nb-cc-s.png
http://www.netbeans.org/products/cplusplus/

Sykora
30-09-2007, 01:21 PM
Anyone here running a 64bit OS who can try that out and tell us what size their int is?

Gigacore
30-09-2007, 06:47 PM
how to use different colors for different letters, while the program is executed.
for example, if i input GIGACORE, it should show all those each every letters in the word GIGACORE with different colors

Sykora
30-09-2007, 06:49 PM
In linux you can use command line ANSI escape sequences, I don't know about windows though.

Gigacore
30-09-2007, 06:53 PM
and which is the best C compiler for linux platform. i've heard about GC++ or something that sounds like that. is it good?

timemachine
30-09-2007, 07:04 PM
its gcc buddy
gcc is a c compiler and g++ is a c++ comiler
trust me, its something awesome. try it and feel its real strength.

in TurboC++ compiler, color scheme is decided by textcolor in text mode.......if in graphics mode, settextstyle is used.

set the logic according to the output and use the above functions. i think it will do the job;)

Gigacore
30-09-2007, 07:10 PM
^ thanks buddy.

i'll try it :)

timemachine
30-09-2007, 07:50 PM
take the help of turboc++ help function
it helps a lot really

Zeeshan Quireshi
30-09-2007, 08:32 PM
If you people really Like TC's Interface then u can use DJGPP alond with the RHIDE IDE , which is a direct vopy of TC's user interface but uses GCC(DJGPP) as it's backend and runs perfectly on Win XP .

www.delorie.com/djgpp/

Note: RHIDE can be downloaded from the "Zip Picker" page of DJGPP Site .

Gigacore
30-09-2007, 08:36 PM
wow, thats cool. thanks buddy

ayush_chh
01-10-2007, 12:43 AM
@shady_inc : Why thank you. :)

@ ayush_chh :
From what I've read of the insnode() function, the code is a mess.

For example, you're mallocing a block of memory at temp->link, without ever initializing temp.

The second thing is, the statement you've bolded, ie n1->link;, isn't supposed to do anything by itself. Perhaps you meant n1->link = temp or something?

EDIT : On further study, I think what you meant was :


if(desti==1)
{
temp=(node *)malloc(sizeof(node));
printf("\nEnter the item to be inserted\n");
scanf("%d",&temp->info);
temp->link=n1;
n=temp;
/*n1->link;*/
printf("\nNode Inserted Successfully\n");
printf("\nThe new Linked List elements are\n");
break;
}


Thanks a lot man.....i understood whr i was wrong


Anyone here running a 64bit OS who can try that out and tell us what size their int is?

don't have one but i can make a guess ,
see if the compiler is 64-bit then
sizeof(int) = 8 byte because 64 bit/8 bit
now limit will be 16^8 = 4294967296

not sure of this........:):)

QwertyManiac
01-10-2007, 01:02 AM
Yeah that'd be obvious but I think Sykora just wants to confirm it or something ...

aditya.shevade
01-10-2007, 01:23 AM
Hey... was out for some work. Nice proceedings going on.

Just to add one more thing to the integer range issue, @QuizMaster or Qwerty (I dunno who mentioned it, since the avatars are verymuch similar), our (not only your) education system is faulty.

And, it is now, that I came to know, that there are people who like TurboC/C++'s crap IDE and the entire package... :-| :confused:

QwertyManiac
01-10-2007, 01:27 AM
Damn Quiz_Master for plagiarizing my genuine™ avatar :(

Hah but my professor hates 'standard'. He says learning all that wasn't necessary in his age and that namespaces etc are just to confuse you, that everything is becoming more technical etc, he hates it :lol:

aditya.shevade
01-10-2007, 12:42 PM
^^ He hates that programming is becoming technical? I don't remember when it wasn't technical.....

Zeeshan Quireshi
01-10-2007, 12:45 PM
And, it is now, that I came to know, that there are people who like TurboC/C++'s crap IDE and the entire package... :-| :confused: Am i mistaken or is it targeted at me :confused:

QwertyManiac
01-10-2007, 01:47 PM
^^ He hates that programming is becoming technical? I don't remember when it wasn't technical.....
Is becoming more technical actually :)

aditya.shevade
01-10-2007, 03:16 PM
Am i mistaken or is it targeted at me :confused:

No. It is not targeted at you. It's your choice what you wanna use.

I am targeting this at some people here (here means where I live) who love it and create compiler specific programs. Like using conio.h and graphics.h etc.

And they make me use it also. And I want freedom. So, I am a bit frustrated.

Is becoming more technical actually :)

:-D.... I can understand ;-)

Zeeshan Quireshi
01-10-2007, 06:45 PM
Dud , i'm totally for Standard C++ , that's why i said that RHIDE has a TC like Interface but uses GCC as backend therefore it is a 'Standard C++' development environment , but with the look and feel like TC :)

timemachine
01-10-2007, 08:31 PM
^^^^^hmmmm gud ........

aditya.shevade
01-10-2007, 09:51 PM
Dud , i'm totally for Standard C++ , that's why i said that RHIDE has a TC like Interface but uses GCC as backend therefore it is a 'Standard C++' development environment , but with the look and feel like TC :)

I know. I said, I am not targeting you but I am targeting those who do not use standard C. You are not included in that. Cool down.

timemachine
01-10-2007, 10:09 PM
leave it dude.......just go for straight discussions... no one is targeting nobody here

QwertyManiac
01-10-2007, 10:14 PM
Yeah, cause ultimately the cleaner code wins, no matter what. :)

timemachine
01-10-2007, 10:17 PM
ya man
but its a problem for all programmers
they all are aggressive:D ;)

Zeeshan Quireshi
02-10-2007, 10:37 AM
I know. I said, I am not targeting you but I am targeting those who do not use standard C. You are not included in that. Cool down.
:D , arre i'm totally Calm n Composed , was just telling ya bout RHIDE .

praka123
04-10-2007, 07:04 AM
Hope this is not a old news ;:)
Introduction to C++ - Stanford Video Tutorials and Other Lectures

Updated September 15, 2007




Here are some of the best rated videos on C++. The first set 5 video tutorials is from reconnetworks.com (http://www.reconnetworks.com/). The next set of 13 lectures from Stanford University is much more in-depth. The lecture at the end is by Dr. Bjarne Stroustrup (http://blogs.icerocket.com/tag/bjarne+stroustrup) - the original designer and implementer of the C++ Programming Language.
link to the videos:
http://idealprogrammer.com/languages/cc/introduction-to-c-standford-video-tutorials-and-other-lectures/

got this via http://linuxhelp.blogspot.com/2007/09/collection-of-best-rated-videos-on-c.html (ofcorz not my blog!)

nithinks
04-10-2007, 08:55 AM
Can anybody write a C program to reverse the contents of a SINGLE Linked list?

Sykora
04-10-2007, 07:22 PM
The only way is to create a new list, going through each node of the previous list and inserting it at the beginning of the new list.

eg :


Node *p = head->next;
Node *revHead = head;
head = head->next;
revHead->next = NULL;
while (p != NULL) {
p = p->next;
head->next = revHead;
revHead = head;
head = p;
}


Now you have a new list, with revHead as the head node.

nithinks
04-10-2007, 07:54 PM
The only way is to create a new list, going through each node of the previous list and inserting it at the beginning of the new list.

eg :


Node *p = head->next;
Node *revHead = head;
head = head->next;
revHead->next = NULL;
while (p != NULL) {
p = p->next;
head->next = revHead;
revHead = head;
head = p;
}


Now you have a new list, with revHead as the head node.

Fine.. but lets assume that list contains some 1 lack records.. so if thats the case this program will create an overhead... any optimistic code?

Sykora
04-10-2007, 08:52 PM
How does it create overhead? It isn't creating new nodes, just reinserting them in a new list.

nithinks
04-10-2007, 09:59 PM
How does it create overhead? It isn't creating new nodes, just reinserting them in a new list.

You are right.. but is it not possible to do that without creating another list?

Nav11aug
04-10-2007, 10:40 PM
try this..


node *revList(node *head)
{
if(head==NULL)
return head;

node *p,*q,*r;
p=head;

while(p!=NULL){
r=q=p->next;
if(q!=NULL){
q->next=p;
p=r;
}
}

head=p;

return head;
}

Glen Apayart
05-10-2007, 03:07 PM
My brother always use my admin account because i leave my pc on sometimes when im away or sleeping for a while.

So, I made a simple conditional statement for my brother to be reminded about the account he is using. By changing the icons of my .exe from his favorite game icon. hehe :D


#include <iostream>
using namespace std;

int main()
{
char Ans;

cout << "Hello!\n";
cout << "The program you have choosen has been disabled by the admin,\n";
cout << "Please use your own account,\n\n";

do{
cout << "do you want to exit (y/n)? ";
cin >> Ans;

if(Ans == 'n' || Ans =='N')
cout << "Could you please leave me now...\n\n";
else if(Ans == 'y' || Ans == 'Y')
break;
else
cout << "Invalid Entry!\n\n";
}
while(Ans != 'y' || Ans != 'Y');
system("cls");
cout << "Thank you!" << "\n\n";
cout << "Press any key to exit...";

return 0;
}

aditya.shevade
05-10-2007, 08:03 PM
^^ ROFL :-D:-D:-D:rolleyes:

By the way, I am working on a Library Emulation project. For my submissions. A mini project. The problem is that everyone has to do this one only (Library) and our mam wants 80 different source codes. (I am sure mine will be unique ;-)).

It does not use file handling. I will post it here soon.....

Aditya

PS @ Glen Apayart, I just saw you signature... You are a newbie you say, but still. There is no void main in ISO C/C++ :-D

BBThumbHealer
05-10-2007, 09:27 PM
i m making my project for class xii...its heading its completion , last thing that i wanna do is like i m making a calendar and a billing dept. calculation center , i wanna print that on a paper using the printer ! is this possible ? if yes , then plz buddies help in coding !

arunks
07-10-2007, 12:18 AM
My brother always use my admin account because i leave my pc on sometimes when im away or sleeping for a while.

So, I made a simple conditional statement for my brother to be reminded about the account he is using. By changing the icons of my .exe from his favorite game icon. hehe :D


#include <iostream>
using namespace std;

int main()
{
char Ans;

cout << "Hello!\n";
cout << "The program you have choosen has been disabled by the admin,\n";
cout << "Please use your own account,\n\n";

do{
cout << "do you want to exit (y/n)? ";
cin >> Ans;

if(Ans == 'n' || Ans =='N')
cout << "Could you please leave me now...\n\n";
else if(Ans == 'y' || Ans == 'Y')
break;
else
cout << "Invalid Entry!\n\n";
}
while(Ans != 'y' || Ans != 'Y');
system("cls");
cout << "Thank you!" << "\n\n";
cout << "Press any key to exit...";

return 0;
}


hey what does system("cls") do...?
is this a inbuilt function...? how and what is its usage..?
is this used to execute shell commands i mean dos commands..

QwertyManiac
07-10-2007, 06:26 AM
hey what does system("cls") do...?
is this a inbuilt function...? how and what is its usage..?
is this used to execute shell commands i mean dos commands..
Yeah it is used to execute commands, or launch other programs too perhaps.

Your current running program halts when this is called, and continues only after the command called completes execution. System() makes the code OS-Dependent.

http://www.phim.unibe.ch/comp_doc/c_manual/C/FUNCTIONS/system.html

Gigacore
07-10-2007, 06:58 AM
what is typedef

QwertyManiac
07-10-2007, 07:27 AM
Typedef is commonly used to give any Data Type a new name.

Like for example:
#include<iostream>
int main()
{
typedef int a; // Thus, a refers automatically to an integer
a abc=1, def=0; // Creates two variables of type int,
// as the typedef above shows
cout<<"ABC = "<<abc<<" DEF = "<<def<<endl;
return 0;
}
This is very useful while working with a lot of classes, or structures. You can name them comfortably or for different usages (Like we use Position and List names in a clear Linked List algorithm but both are essentially the same structure, just used with a different name to aide understanding).

Gigacore
07-10-2007, 09:03 AM
thanks qwerty :)

shyamno
07-10-2007, 10:12 AM
can anyone give me a tutorial on how to ....do user authentication using C..is it possible...

suppose I ..have created a website ...where a member have to login...so how do I check the user verification.....

Any source code...

BBThumbHealer
07-10-2007, 12:48 PM
i m making my project for class xii...its heading its completion , last thing that i wanna do is like i m making a calendar and a billing dept. calculation center , i wanna print that on a paper using the printer ! is this possible ? if yes , then plz buddies help in coding !

wud anyone lyk to help in the above case ?

aditya.shevade
08-10-2007, 01:00 PM
I have been having a problem. And someone please help me as soon as you can.

I have designed a project to be submitted in the college for submission purpose. Now, the project is a library database. I have developed a little part and I am attaching it.

When I showed the program to our teacher, she said that I must use inheritance in the program. The problem is that I don't know much about it, and from whatever I read and tried to gather in the last 2 days, I have reached a conclusion that inheritance cannot be used effectively in this case.

So please please tell me where and how to use it. Else I will have to submit this one only.

Aditya

QwertyManiac
08-10-2007, 06:27 PM
Sometimes in colleges, 'effective' isn't the word, its 'you have to'. So implementing it even for a basic thing would satisfy them. Just saying ..

I don't understand 'make' stuff in your files, you use Ajunta? Is coding them all into one file not a good procedure? And the use of header files etc? Wait, how do I even compile it? :confused: Need some enlightenment on this.

aditya.shevade
08-10-2007, 06:36 PM
Sometimes in colleges, 'effective' isn't the word, its 'you have to'. So implementing it even for a basic thing would satisfy them. Just saying ..

I don't understand 'make' stuff in your files, you use Ajunta? Is coding them all into one file not a good procedure? And the use of header files etc? Wait, how do I even compile it? :confused: Need some enlightenment on this.

I use anjuta. You guessed right. To compile it... I dunno the sommand line method. But if you have Anjuta, then create a new project and add the files in that project. Then click on auto generate the project.

Then compile, make and build to execute.

And after reading professional C++, I have reached the conclusion that creating different files is the best way to make the program easy to understand, modify and expand.

Try if you can, else I will send you the entire project by mail. The compressed (tar.bz2) file is about 1000KB.

Aditya

[xubz]
08-10-2007, 06:39 PM
Has anyone correctly implimented Brent-Salamin algorithm for computing Pi? I never was able to get it right :(

(Only thing i've done is Leibniz method of Pi, thats easy, but it takes insane amount to just calculate 10 decimal places accurately! :-/)

The_Devil_Himself
08-10-2007, 06:42 PM
^^lol me too.I am wondering how to compile it using Dev c++.
BTW you are very good at programming Aditya and a clean programmer.

QwertyManiac
08-10-2007, 09:33 PM
Try if you can, else I will send you the entire project by mail. The compressed (tar.bz2) file is about 1000KB. That's okay, never mind, seems beyond my mind right now. Someone else like Yamaraj or Sykora might be able to answer it I s'pose. Am not that good really, yet :p

And a doubt:
Does Anjuta automatically create Header files or did you _HAVE_ to code them?

aditya.shevade
08-10-2007, 09:44 PM
^^ :-(

QwertyManiac
08-10-2007, 09:47 PM
Alright, am trying my best right now. (I sure am tired after this (http://www.thinkdigit.com/forum/showpost.php?p=626257&postcount=51)).

Hopefully would tell you a few ways to implement inheritance into it after going through entirely.

/Me does a sudo apt-get install anjuta

Edit: You need to document your files .. Comments make it easier :(

aditya.shevade
08-10-2007, 09:53 PM
^^ Anjuta is very good.

And I need this by friday. :-|

vinit suri
10-10-2007, 08:07 PM
hey guys i needed ur help wid a c program.....

cud sm1 plzz post dis program 4 me...plzzzzz
d program is that....

it should accept any number between one and thousand and find the first non zero digit of the factorial of dat number from the right hand side.....


plz plz plz help me...

mehulved
10-10-2007, 08:14 PM
Shouldn't that be easy.
1) Find the factorial
2) Divide it by 10
3) Check the remainder
4) If it's 0 discard it

vinit suri
10-10-2007, 08:44 PM
d question is which datatype 2 use....
v are talkin abt d factorial of even 1000....
so ders no datatype which can store dat....finding factorial is impossible....

hey guys i needed ur help wid a c program.....

cud sm1 plzz post dis program 4 me...plzzzzz
d program is that....

it should accept any number between one and thousand and find the first non zero digit of the factorial of dat number from the right hand side.....


plz plz plz help me...
__________________

The_Devil_Himself
10-10-2007, 08:48 PM
You can use exponential form i.e. aeb= a into e raised to the power b.Its limits is from -3.4e38 to 3.4e38.Hope this helps.I have never used it so don't ask how to implement it.

BTW your question looks like picked up from CAT or something.I'm sure of some trick involved.

aditya.shevade
10-10-2007, 09:11 PM
^^ trick might be that, if you multiply by 1000 then the first non zero from right cannot be 1 to 3 as there will be three zeros already. Also, for 1000, you will have 990 so add one more zero, 980 another zero and so on.

It's not easy this way, but worth a try.

aditya.shevade
10-10-2007, 09:19 PM
Edit: You need to document your files .. Comments make it easier :(

Actually, even I thought I should but after a couple of my friends who are not very good at C/C++ also understood the code just be the names of the variables, I think I can save the trouble.

So, I just added a naming conventions file as a documentation, and forgot to add it in the zip archive.

Here it is by the way, if you would like.

Aditya

Batistabomb
11-10-2007, 06:09 PM
do namespacestd,exception handling concepts are working for you in turbo c++ on windows platform ?

Hi guys n gals,

If you are a good at C/C++ Programming or if you are a programmer or just know this language then post you Programs here. By this way it helps learners a lot. Members can post their programs and get suggestions if there is anything wrong it......

Use this Pattern:

A Program to find the largest of three numbers using nexted if

#include <stdio.h>
main()
{
int a,b,c,big;
printf("Enter Three numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
if(a>c)
big=a;
else
big=c;
else
if(b>c)
big=b;
else
big=c;
printf("Largest of %d,%d and %d = %d",a,b,c,big);
return 0;
}


great thought dude your's going to be the highest replies thread and most helpful in terms of exchanging knowledge

quan chi
12-10-2007, 02:40 PM
guys i have a problem.refer the program below.

i am starting from main.
void main()
{char name[30]
int x,y;
cout<<"enter your name"
cin>>name;
cout<<"enter your date of birth month and day only";
cin>>x;
cin>>y;
cout<<"****************"<<name<<"************* "
cout<<" your date of birth is"<<x<<" "<<y;
if(x==2&y==3)
{cout<<"some result"
}
else
{cout<<"some result"
}
}

now in this program if i enter the name as "abceg"

the program outputs properly.

but if i enter something like "abced ghtyat"
it only takes my name it asks for dob but dosent take any input.
and take the dob as any garbage value by itself.
and outputs the else statements result.

please help.

The_Devil_Himself
12-10-2007, 03:05 PM
^^dude I think you can't use cin to input strings.use gets to input full names(i.e. with space in between).

Batistabomb
12-10-2007, 03:47 PM
^^dude I think you can't use cin to input strings.use gets to input full names(i.e. with space in between).
you are right cin inputs strings but wont take spaces in the above program if you are typing your name with spaces then suddenly cin looks for the next cin i.e; x,y here
you can verify this by typing in output like this

abcd ghyt 1 2

so cout here is being neglected,so keep gets

#include<iostream.h>
#include<stdio.h>
void main()
{
char name[30];
int x,y;
cout<<"enter your name";
gets(name);
fflush(stdin);
//cout<<endl;
cout<<"enter your date of birth month and day only"<<endl;
cin>>x;
cin>>y;
cout<<"****************"<<name<<"************* ";
cout<<" your date of birth is"<<x<<" "<<y;
}
/*if(x==2&y==3)

cout<<"some result"

else

cout<<"some result"

} */

Sykora
12-10-2007, 05:56 PM
If you're programming in C, use gets(). If you're programming in C++, use cin.getline()

Tech.Masti
12-10-2007, 07:03 PM
anyone tell me a good C++ problem book name for beginner.......

The_Devil_Himself
12-10-2007, 07:20 PM
^^ robert lafore.

aditya.shevade
12-10-2007, 08:18 PM
If you're programming in C, use gets(). If you're programming in C++, use cin.getline()

I had been wondering about that... thanks. Needs some digging....

Desi-Tek.com
12-10-2007, 09:42 PM
http://www.cplusplus.com/

http://www.cplusplus.com/doc/tutorial/

aditya.shevade
12-10-2007, 11:22 PM
If you're programming in C, use gets(). If you're programming in C++, use cin.getline()

Hey... I did some digging as I said. And if you are using the string input type, then go for getline. Simple getline, not cin.getline.

The syntax is,

string someString;

std::getline(cin, someString);

cin is the same. It's better than cin.getline();

And you should use cin.ignore(); before the getline to make sure that stdin is clear.

Aditya

Sykora
12-10-2007, 11:39 PM
You're right. getline is used with the C++ std::string while cin.getline() is used with char*. You should use std::string whenever possible, but if you're dealing with char* (for whichever reason), use cin.getline().

Gigacore
13-10-2007, 02:30 PM
how to declare `feof`

aditya.shevade
13-10-2007, 03:51 PM
What do you mean by declare feof?

Syntax of use if feof(file poniter name) and if you mean the key sequence for end of file then it is ctrl+z in windows/dos and ctrl+d in *nix OS.

EDIT :- the syntax of use is feof(file pointer name)

Gigacore
13-10-2007, 04:39 PM
lol yeah i mean the syntax :D
anyway thankx.

I'M A NOOB

its me
21-10-2007, 02:43 AM
Hello



I need a program in C++ that convert numbers from Hexadecimal to Binary


thanks :p

nightcrawler
22-10-2007, 02:18 PM
Hello



I need a program in C++ that convert numbers from Hexadecimal to Binary


thanks :p

1. Get Input from user in Hexadecimal format. The C scanf() with %x works in C. I am sure cin can directly take input in hexadecimal. BTW you have to preceed the input with a 0xNumber for example 0x1F for 31.

2. Once you have the number apply bit shifting logic or normal method of divide and mod. And get the binary number (that is if you wish to store it).

If on the other hand if you wish just to print it I think cout lets you do that like it lets you print in hex. Although I am not sure if it can print in binary.

Correct me if I am wrong guys.

Incidentally why would you want to convert a number from hex to binary ?

ilugd
22-10-2007, 02:58 PM
much simpler. One hx digit is 4 binary. Just create an array of 16 which matches each hexadecimal character to its binary equivalent and print it out. It is not puritan but gets the job done.

its me
22-10-2007, 06:26 PM
Thank you very much :)

quan chi
26-10-2007, 08:30 PM
guys i have a problem please help.
well can anyone please give me a program on 'classes with other classses as member data'.with a bit of explanation.

for eg you can take a rectangle and point class.

actually there is a program for it but i am not able to get it properly
can anyone please explain it me.
here is the program.

class point
{private:
int itsx; //to hold the x and y co-ordinate
int itsy;
public:
void setx(int x){itsx=x}
void sety(int y){itsy=y}
int getx()const{return itsx;}
int gety()const{return itsy;}
};

class rectangle
{private:

point itsupperleft; /* from here i am not understanding.if these are of
point itsupperright; type point then why the below its top bottom left etc
point itslowerleft; has been defined*/
point itslowerright;

int itstop;
int itsleft;
int itsbottom;
int its right;

public:
rectangle(int top,int left,int bottom,int right);
~rectangle(){}

*********the accessor functions defined here********

int getarea() const;

};

rectangle::rectangle(int top,etc etc.....)
{itstop=top;
itsleft=left;
etc etc.......;

itsupperleft.setx(left); /*why these declarations */
itsupperleft.sety(top);

itsupperright.setx(right);
itsupperright.sety(top);

etc etc.....;
}
int rectangle::getarea()const
{int width=its right-its left; /*how they are calculating it
int height=its top-its bottom; are they subtracting the two opposite
return(width*height); points*/

int main()
{body etc etc.......
}


i have pointed my doubts in the program only.
is there any other simpler way to write this program.or can anybody post any other simpler program.

i reffered sams publications jesse liberty and bradley jones book.pg no.165.

please help.

CadCrazy
28-10-2007, 04:50 PM
Here is mine


#include<stdio.h>

void main()
{
int Money, GirlFriends;

while(Money!=0)
{
GirlFriends++;
Money--;
}
printf("Beggar");

getch();
}

:D

mehulved
28-10-2007, 08:36 PM
Is it legal to distribute TC?

gaurav_indian
28-10-2007, 08:38 PM
Is it legal to distribute TC?
No.Its not legal.

Gigacore
28-10-2007, 08:40 PM
oops.... i dont know... if it is illegalll.. please remove it :(

gaurav_indian
28-10-2007, 08:41 PM
oops.... i dont know... if it is illegalll.. please remove it :(
Edit your post.:p I hate piracy.:mad:

Gigacore
28-10-2007, 08:43 PM
^ so is it legal ?

CadCrazy
28-10-2007, 09:30 PM
I hate piracy.:mad:
Abe 100 chuhe kha ke billi haj ko chali. Haj se vapis aa ke phir chuhe khane lagi

gaurav_indian
28-10-2007, 09:32 PM
Abe 100 chuhe kha ke billi haj ko chali. Haj se vapis aa ke phir chuhe khane lagi
sunday ko holiday hoti hai.:D

CadCrazy
28-10-2007, 09:34 PM
to kya

QwertyManiac
28-10-2007, 09:39 PM
Some sites have the v3.0 in their abandonware list .. But Borland still has a page for it OUTSIDE the museum... I wouldn't know if its perfectly legal or not to distribute it now. But frankly, I cant stand that stupid IDE anymore. Its like staring at a BSOD for 5 hours only to build something equivalent of giving a BSOD in itself. Wonder why people don't move on ... :?

aditya.shevade
28-10-2007, 10:36 PM
Maybe because of the authors of books (indian) which emphasize on TC. Or maybe, like here in my case, my friends use it cause their classes have it. Or maybe because they have to use it in their colleges.

I hate it by the way.

Gigacore
29-10-2007, 07:37 AM
A C Program to check whether a given word is Palindrome or not.

Example for Palindromes: MADAM, MALAYALAM, LEVEL, RADAR etc...

#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char str[10],str2[10];
printf("Enter a string");
scanf("%",str1);
strcpy(str2,str1);
strrev(str2);
if(strcmp(str1,str2)==0)
printf("%s is a palindrome",str1);
else
printf("%s is not a palindrome",str1);
getch();
}

mehulved
29-10-2007, 09:40 AM
Reading this wikipedia article (http://en.wikipedia.org/wiki/Turbo_C++), I feel it's in a grey area so I am deleting the post. If someone can give me the proof that it's legal, I will restore it.

Gigacore
29-10-2007, 09:58 AM
i think u dont need to delete it... coz i edited the post:D

mehulved
29-10-2007, 10:04 AM
...while I was deleting it.

ayush_chh
29-10-2007, 04:11 PM
@ gigacore

waht did ya post BTW????

and what is it all abt??

QwertyManiac
29-10-2007, 04:34 PM
Link to Turbo C++ 3.0.

Gigacore
30-10-2007, 07:17 PM
A Simple C Program to Display the number and its square from 1 to 10 using register variable.

#include <stdio.h>
#include<conio.h>
void main()
{
register int count, sqr;
for(count=1;count<=10;count++)
{
sqr=count*count;
printf("\n%d%d",count,sqr);
}
getch();
}

EDIT: I added a list of all the PROGRAMS in this thread (http://www.thinkdigit.com/forum/showthread.php?t=67133)

aditya.shevade
30-10-2007, 07:41 PM
@Gigacore, Great work man...

Gigacore
30-10-2007, 08:04 PM
^ Thanks a lot Buddy

quan chi
30-10-2007, 09:11 PM
greetings guys.
well in an urge to make a matrix falling numbers program.(like in the movie):D .i made a simple c++ program which is as follows

#include<iostream.h>
void main()
{
int counter=0;
loop:
counter++;
cout<<counter<<endl;
goto loop;
}

now the problem is the numbers move from below to up.

and how to exit from this program.once run this program rufuses to exitas it has no termination for loop.:D .

please help me.is there any way by which by pressing 'esc' as like in other programs i an exit from this program.. :o

QwertyManiac
30-10-2007, 09:15 PM
Ctrl+Pause|Break or just close that Window. Yours is not a good way to implement this.

Have a look at programs like cmatrix, which create the exact effect. You can source install it on Linux. Or see here:
http://www.asty.org/cmatrix.html

harryneopotter
30-10-2007, 09:34 PM
#include<iostream.h>
void main()
{
int counter=0;
while(!kbhit())
{
counter++;
cout<<counter<<endl;
}
}

use this.................it will exit on any key press.

blackleopard92
30-10-2007, 09:49 PM
A Simple C Program to Display the number and its square from 1 to 10 using register variable.

#include <stdio.h>
#include<conio.h>
void main()
{
register int count, sqr;
for(count=1;count<=10;count++)
{
sqr=count*count;
printf("\n%d%d",count,sqr);
}
getch();
}

EDIT: I added a list of all the PROGRAMS in this thread (http://www.thinkdigit.com/forum/showthread.php?t=67133)
won't recommend this method in C++, as modern compilers automatically optimise for loops with universal register counters.

quan chi
30-10-2007, 11:47 PM
#include<iostream.h>
void main()
{
int counter=0;
while(!kbhit())
{
counter++;
cout<<counter<<endl;
}
}

use this.................it will exit on any key press.

thanks harryneopotter

Ctrl+Pause|Break or just close that Window. Yours is not a good way to implement this.

Have a look at programs like cmatrix, which create the exact effect. You can source install it on Linux. Or see here:
http://www.asty.org/cmatrix.html

well thanks for the link.i have downloaded it but how to run it in windows please explain in detail.

Gigacore
06-11-2007, 07:51 AM
A C Program to find the sum of two matrix using two dimensional array.

#include <stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,n,m;
printf("Enter the order of matrix:);
scanf("%d%d",&n,&m);
printf("Enter the elements of matrix")
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Addition of matrices");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
return(0);
getch();
}

QwertyManiac
06-11-2007, 08:47 AM
^^
Correction, its 'matrices' and not 'matrixis' :)

Gigacore
06-11-2007, 08:48 AM
^ Oh thanks qwerty :)

mehulved
06-11-2007, 10:06 AM
^ Oh thanks qwerty :) ....the spelling nazi!

QwertyManiac
06-11-2007, 10:49 AM
Won't learning the right stuff help? :p

Gigacore
06-11-2007, 06:01 PM
@ mehul... its common to make mistakes sometimes :D .

MetalheadGautham
13-11-2007, 10:17 PM
TURBO CALC 15000


This is a basic calculator compiled using GCC, but first written using Borland's Turbo C++ IDE 3.0, hence called turbo calc. It has 15 functions, hence 15000

/*
Turbo Calc 11000 Coppyright(c) Gautham T

This is freeware, use this software as you
like, but the author must be attributed to
when making derivatives. This source code may
also be distributed with it. Its delivered under
GNU GPL Version 3, a coppy of which may be found in
www.gnu.org . This software is Designed to
illustrate some basic operations with C++
and it uses Turbo C++ IDE 3.0 for compiling
although source code is easily ported to
other platforms.
*/

#include<iostream.h>
#include<math.h>

/* iostream.h for basic operations
conio.h for clrscr()
math.h for many of the calculator's functions */

int main() //calculator function

{

long double a,b; //the two operants

unsigned short int c; //choice

cout<<"This is turbocalc 11000, your best choice for a"<<endl;
cout<<"simple and robust x86 platform based calculator."<<endl<<endl;

cout<<"Type 1 for addition;"<<endl; //add
cout<<"Type 2 for subtraction;"<<endl; //sub
cout<<"Type 3 for multiplication;"<<endl; //multi
cout<<"Type 4 for dividion;"<<endl; //div
cout<<"Type 5 for finding the Square;"<<endl; //square
cout<<"Type 6 for finding the Cube;"<<endl; //cube
cout<<"Type 7 for finding the square root;"<<endl; //root
cout<<"Type 8 for finding the sine;"<<endl; //sin
cout<<"Type 9 for finding the cosine;"<<endl; //cos
cout<<"Type 10 for finding the tangent;"<<endl; //tan
cout<<"Type 11 for finding the seccant;"<<endl; //sec
cout<<"type 12 for finding the coseccant;"<<endl; //cosec
cout<<"Type 13 for finding the cotangent;"<<endl; //cot
cout<<"Type 14 for finding the natural logarithm;"<<endl; //log
cout<<"Type 15 for finding the logarithm to base 10."<<endl; //log10

cin>>c; //got the choice now

if ((c==1)||(c==2)||(c==3)||(c==4))
{
cout<<"Enter the first number:"<<endl;
cin>>a;
cout<<endl<<endl<<"Enter the second number:"<<endl;
cin>>b;
}
else if((c==5)||(c==6)||(c==7)||(c==8)||(c==9)||(c==10) ||(c==11)||(c==12)||(c==13)||(c==14)||(c==15))
{
cout<<"enter the number: "<<endl;
cin>>a;
}
else
{
cout<<"You must only enter a number from 1 to 15."<<endl;
}

if (c==1)
cout<<"The Sum is "<<a+b<<endl;

else if (c==2)
cout<<"The Difference is "<<a-b<<endl;

else if (c==3)
cout<<"The Product is "<<a*b<<endl;

else if (c==4)
cout<<"The Quotient is "<<a/b<<endl;

else if (c==5)
cout<<"The Square is "<<a*a<<endl;

else if (c==6)
cout<<"The Cube is "<<a*a*a<<endl;

else if (c==7)
cout<<"The Square Root is "<<sqrt(a)<<endl;

else if (c==8)
cout<<"The Sine is "<<sin(a)<<endl;

else if (c==9)
cout<<"The Cosine is "<<cos(a)<<endl;

else if (c==10)
cout<<"The Tangent is "<<tan(a)<<endl;

else if (c==11)
cout<<"The Seccant is "<<1/(cos(a))<<endl;

else if (c==12)
cout<<"The Coseccant is "<<1/(sin(a))<<endl;

else if (c==13)
cout<<"The Cotangent is "<<1/(tan(a))<<endl;

else if (c==14)
cout<<"The Natural Logarithm is "<<log(a)<<endl;

else if (c==15)
cout<<"The Logarithm to base 10 is "<<log10(a)<<endl;

else
cout<<"There must be an error in what you typed."<<endl<<"Try Again.";

return 0;
}
PS: I am a rookie C++ programmer. I had learnt C in the begening of class 6(lol) but forgot it(lol again)

I made this because I just wanted to try something that looks usable. Please excuse my n00bishness

I regret the fact that we have only Turbo C++ in CBSE senior secondary scyllabus. Anjuta IDE and Dev-Cpp along with MinGW Developer Studio are forgotton. CBSE I suppose, is full fo linux haters.

Nav11aug
14-11-2007, 04:23 AM
I dunno if it has Linux-haters or Linux-illiterates, and puttin in Linux won't help. We have enough dumb teachers anyway

Gigacore
14-11-2007, 07:49 AM
did anyone try the turbo calc ?

i've prob with my compiler.. :(

[xubz]
14-11-2007, 10:32 AM
did anyone try the turbo calc ?

i've prob with my compiler.. :(
Working Fine here.

Used Borland C++ 5.5 and VIM (heh, I use vim even in windows ^_^)

@MetalheadGautham -> You can use Switch-Case instead of Insane no if nested-if for just validating a single number :)

Gigacore
14-11-2007, 11:05 AM
It worked now for me in Dev C++.. nice program.. keep it up!

MetalheadGautham
14-11-2007, 04:04 PM
You can use Switch-Case instead of Insane no if nested-if for just validating a single number
know it... but I still prefer if clause...

It worked now for me in Dev C++.. nice program.. keep it up! nice program? isn't this supposed to be a n00bish bit of code?(what else can you expect from a cbse programmer?)

Sykora
14-11-2007, 04:56 PM
Offtopic :

nice program? isn't this supposed to be a n00bish bit of code?(what else can you expect from a cbse programmer?)

Oi! I may decide to take that as an insult :(

"Noobish code" is where you start on your way to greatness. Everyone does noobish code at the beginning. Just don't stay a noob for too long :) .

Ontopic : How can you say it compiled with gcc if you used nonstandard header files and outdated syntax?

QwertyManiac
14-11-2007, 05:07 PM
know it... but I still prefer if clause...A switch() block is perhaps faster than an if-else cascading block. Its something to do with jump tables. C compilers automatically optimize a switch block into a branch/jump table.

But of course, it (switch) has its limitations too, like handling only integral data types (No strings, etc).

[xubz]
14-11-2007, 07:57 PM
A Command Line Calculator.

I coded this because sometimes if I quickly wanted to multiply/divide some numbers, I'd hate to open up the Calculator App (especially in Linux).

Just cal 2 + 2 or cal 100 / 39 would throw up the result. Much Better :)

#include<stdio.h>
#include<stdlib.h>

int main(int argc, char *argv[])
{
float a, b;
char op;

if (argc < 4 || argc > 4)
{
printf("\nUsage: cal <number1> <operator> <number2>\n");
printf("\nValid Expressions: + - * /\n");
printf("\nExample: cal 2 + 2\n");
exit(1);
}

/* Store the Command Line Arguments in Local Variables */
a = atof(argv[1]);
b = atof(argv[3]);
op = *argv[2];

switch(op)
{
case '+':
printf("\n%f", (float)a+b);
break;

case '-':
printf("\n%f", (float)a-b);
break;

case '*':
printf("\n%f", (float)a*b);
break;

case '/':
printf("\n%f", (float)a/b);
break;

default:
printf("\nInvalid Operator");
break;
}

printf("\n");
return(0);
}


Tested to compile and work using GCC under Linux

QwertyManiac
14-11-2007, 08:28 PM
']I'd hate to open up the Calculator App (especially in Linux).You can use the bash math commands like bc, it supports floating point calculations.

Use bc filename if you have the math expressions stored in a file line by line.

Or use bc <enter> to enter the interactive mode.

Or even use echo expression | bc to pipe it to bc without a file or interaction.

Don't forget to use man bc or info bc ;)

The other bash commands I know of are dc (reverse polish calculator) and factor (splits a number to its factors).

I think there's also a package for scientific calculation via CLI, its called wcalc.

mehulved
14-11-2007, 10:00 PM
Or use python

QwertyManiac
14-11-2007, 10:23 PM
Python loaded with os module (>>> import os) would be great, run bash and python commands together from within the python shell :D

Sykora
15-11-2007, 08:33 AM
But using the output of shell commands within python is moderately difficult. Although the os module does give you the tools, it takes some time to learn them. (os.popen)

It's a long time since I used a "calculator" app. python with ipython shell is bliss. So is sage.

[xubz]
15-11-2007, 08:47 AM
Hmmm, Python Shells, Google-ing revealed PyShell, Lemme Try it :P

QwertyManiac
15-11-2007, 08:51 AM
Yeah PyShell is there too, but its sort of weak at its work. Don't use it as a replacement for bash! :p

srikanth.9849671439
29-11-2007, 05:45 PM
1)wrap such that the oupput should be az,by,cx,dw.........................
2)wrap such that the oup put should be 1+10,2+9,3+8..........5+6
3)print the numbers in pyramid shape.......
1
2 2
3 3 3
4 4 4 4
4) print
y
z y z
r z y z r
z y z
y

sandeep9796
20-12-2007, 02:49 AM
hmmmmmmmmmm

i m tryin to make and application tht will generate a listin of files in a directory and display them????

anyone knows how to do it???

mehulved
25-12-2007, 07:54 PM
hmmmmmmmmmm

i m tryin to make and application tht will generate a listin of files in a directory and display them????

anyone knows how to do it???
I guess this should help http://minnie.tuhs.org/UnixTree/V7/usr/src/cmd/ls.c.html

nvidia
25-12-2007, 08:05 PM
3)print the numbers in pyramid shape.......
1
2 2
3 3 3
4 4 4 4

I dont know how to do the first 2 and the 4th one. Here is the answer to the 3rd:

#include <stdio.h>
#include <conio.h>

void main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
printf("%d\t" , i);
printf("\n");
}
getch();
}

girish.g
08-01-2008, 07:54 PM
please help in the following programs
1 finding triangle matrix in a given matrix
2 to find sum of series: x-(x^2)/3!+(x^3)/5!-(x^4)/7!+(x^5)/9!.......... upto n terms(where x^2 is x square x^3 is x cube and so on and ! is factorial)
3 to convert binary to decimal
please this is urgent as i have to submit a project in school

Sykora
08-01-2008, 09:06 PM
please this is urgent as i have to submit a project in school

Thanks for saving us the trouble of finding out if it was homework...

T159
08-01-2008, 09:15 PM
Thanks for saving us the trouble of finding out if it was homework...
amen:D

Gigacore
08-01-2008, 10:12 PM
I forgot this thread... anyway nice discussion going on

arun_cool
10-01-2008, 09:25 PM
Guys pls post some simple games developed in c\c++ ......

i need it for my Software engineering lab....

pls guys help me out:cry::cry::cry:

shady_inc
21-01-2008, 05:38 PM
Is there any way to fill an array with random numbers using loops.??For example if I have to write table of 7 the program wll be:

int i,array[10];
for(i=0;i<10;i++)
{ array[i]=i*7+7;}

But this will print out multiples of seven.What if I want to fill the array with random numbers like 4,77,665,34 etc.??Is adding each data to array the only option here.??

Also, why is this program not working.??

#include <iostream>
using namespace std;
int main()
{ xyz:
float asc[10],num,sub;
int i,j,p;
cout<<"Enter 10 numbers in any order:\n";
for(i=0;i<10;i++)
{ cin>>asc[i];}
cout<<"Enter the number to be searched:"<<endl;
cin>>p;
for(j=0;j<10;j++)
{ sub=asc[i]-p;
if(sub=0)
cout<<"The number is at"<<i<<"th position.";
else
cout<<"Number not found.";
cin.get();
}
goto xyz;
}

QwertyManiac
21-01-2008, 06:38 PM
#include <iostream>
using namespace std;
int main()
{ xyz:
float asc[10],num,sub;
int i,j,p;
cout<<"Enter 10 numbers in any order:\n";
for(i=0;i<10;i++)
{ cin>>asc[i];}
cout<<"Enter the number to be searched:"<<endl;
cin>>p;
for(j=0;j<10;j++)
{ sub=asc[i]-p;
if(sub==0)
cout<<"The number is at"<<i<<"th position.";
else
cout<<"Number not found.";
cin.get();
}
goto xyz;
}

Pay attention to comparators ;)

(BTW, why do TWO proceses to match an item? Just one check would do right? Why subtract to 0, and then deduct if its a right result or wrong, when you could just directly check input with each in the array and say the same?)

As for random numbers, use the cstdlib provided rand() and srand() functions to generate random numbers.

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
int random_integer = rand();
int a[10];
for (int i=0;i<10;i++)
a[i]=rand();
for (int i=0;i<10;i++)
cout<<a[i]<<endl;
return 0;
}

Edit: Wait, I ran your code, its bad. Your logic is sorta flawed in the loops, check it. And please, never ever use goto.

Here, try this:
#include<iostream>

using namespace std;

int main()
{
int a[10],i,j,check;

cout<<"Enter 10 values: ";
for (i=0;i<10;i++)
cin>>a[i];

cout<<"Enter the number to check: ";
cin>>check;

for (i=0;i<10;i++)
{
if(a[i]==check)
{
cout<<"Number found at "<<i+1<<"th position.";
break;
}
else if(i==9)
cout<<"Number not found.";
}

return 0;
}

shady_inc
21-01-2008, 07:12 PM
Hmm.goto is an easy way to get from anywhere to anywhere in a program.But I guess it's always better to use break; to get outta loops.
As for the random numbers part, I think I wasn't vey clear in the first post.Suppose I have 5 fixed random values,say 1,3,8,3,7 and I want to put them in array, how would I do it.??rand() function will select any 5 random values generated from it's algorithm and fill them in array.That's not what I want ..

ilugd
22-01-2008, 01:28 PM
something like this will work?

#include <iostream>

using namespace std;

int main()
{
int i, array[10],randomarray[10];
cout<<"Enter 10 numbers in any order";
for (i=0; i<10; i++) {cin>>array[i];randomarray[i]=0;}
for (i=0;i<10;i++){
int r=rand()*10;
if(randomarray[r]<>0
randomarray[r]=array[i];
else
i--;
}
for(i=0;i<10;i++) cout<<randomarray[i];
}

Jove
30-01-2008, 11:42 AM
hi there....if u want to fill an array with random numbers, one can simply use rand() function na...and if at all u need to have upper limit over the range of value,rand()%UPPERLIMIT, does it...and still if u want to have different numbers(without repetition), with some trade off between time and space,define an array like,

int array[MAX]; //where MAX is upper limit....
//then
while(count<=reqCount)
{
int c=rand();
if(array[c%MAX]!=c%MAX)
array[c%MAX]=c%MAX,count++;
}

guess this does the required task..

cheers :)

mavihs
07-02-2008, 08:12 PM
function 2 input a matrix:-

void input(int A[10][10], int &R, int &C)
{
int i, j;
cout<<"enter the no. of rows:";
cin>>R;
cout<<"Enter the no. of columns:";
cin>>C;
for(i=0; i<R; i++)
{
for(j=0; j<C; j++)
{
cin>>A[i][j];
}
}
}

mavihs
18-02-2008, 11:34 PM
/* WAP to execute the following function:
-> Read an integer & an integer array & search for that integer in the intger array & return its position, incase the number is not there in the list, function should return -1.*/

int search(int siz, int ele, int arr[]);
void main()
{
clrscr();
int arr[25], siz, i, ele, j;
cout<<"Enter the number of elements:";
cin>>siz;
cout<<"Enter the array:";
for(i=0; i<siz; i++)
{
cin>>arr[i];
cout<<"\t";
}
cout<<"Enter the element to be searched";
cin>>ele;
k=search(siz, ele, arr);
if(j==-1)
cout<<"Element not found";
else
cout<<"The element found at"<<j<<"th position";
getch();
}
int search(int siz, int ele, int arr[])
{
int pos=-1;
for(int i=0; i<siz; i++)
{
if(ele==arr[i])
{
pos=i;
break;
}
else
pos=2;
}
if(pos==1)
{
return(i+1);
}
else
{
return(-1);
}
}

QwertyManiac
19-02-2008, 12:33 AM
/* WAP to execute the following function:
-> Read an integer & an integer array & search for that integer in the intger array & return its position, incase the number is not there in the list, function should return -1.*/

int search(int siz, int ele, int arr[]);
void main()
{
clrscr();
int arr[25], siz, i, ele, j;
cout<<"Enter the number of elements:";
cin>>siz;
cout<<"Enter the array:";
for(i=0; i<siz; i++)
{
cin>>arr[i];
cout<<"\t";
}
cout<<"Enter the element to be searched";
cin>>ele;
k=search(siz, ele, arr);
if(j==-1)
cout<<"Element not found";
else
cout<<"The element found at"<<j<<"th position";
getch();
}
int search(int siz, int ele, int arr[])
{
int pos=-1;
for(int i=0; i<siz; i++)
{
if(ele==arr[i])
{
pos=i;
break;
}
else
pos=2;
}
if(pos==1)
{
return(i+1);
}
else
{
return(-1);
}
}

The entire thing in simple Python: (Thought it might help the bunch of shifters here)

n = int(raw_input("Enter the no of elements: "))
print "Enter the elements:"
a = [int(raw_input()) for x in range(0,n)]
c = int(raw_input("Enter a number to search: "))
print a.index(c) if c in a else -1

Pathik
19-02-2008, 11:51 AM
EDIT:
The entire thing in simple Python: (Thought it might help the bunch of shifters here)

^^ I think it s helping me already.. :D

QwertyManiac
19-02-2008, 05:41 PM
What wasn't working in the first place?

grvpuri
21-02-2008, 08:27 PM
//Program to perform operations on a binary search tree

#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *left;
node *right;
};
class bstree
{
private:
node *root;
public:
bstree()
{
root=NULL;
}
~bstree()
{
cout<<"\nDestructor called\n";
inorder_destroy(root);
}
node* search(int,node*&)const;
void insert(int);
void del(int);
void traverse()const;
void inorder(node*)const;
void preorder(node*)const;
void postorder(node*)const;
void inorder_destroy(node*);
};
node* bstree::search(int item,node *&parent)const
{
node *ptr=NULL;
parent=NULL;
if(root!=NULL)
{
ptr=root;
parent=NULL;
while(ptr!=NULL)
{
if(item==ptr->data)
return ptr;
parent=ptr;
if(item<ptr->data)
ptr=ptr->left;
else
ptr=ptr->right;
}
}
return ptr;
}
void bstree::insert(int item)
{
node *parent;
if(search(item,parent)!=NULL)
{
cout<<"\nSuch a node already exists\n";
getch();
return;
}
node *temp=new node;
temp->data=item;
temp->left=NULL;
temp->right=NULL;
if(parent==NULL)
{
root=temp;
return;
}
if(item<parent->data)
parent->left=temp;
else
parent->right=temp;
}
void bstree::traverse()const
{
if(root==NULL)
{
cout<<"\nTree empty\n";
getch();
return;
}
cout<<"\nInorder\n\n";
inorder(root);
cout<<"\nPreorder\n\n";
preorder(root);
cout<<"\nPostorder\n\n";
postorder(root);
getch();
}
void bstree::inorder(node *n)const
{
if(n!=NULL)
{
inorder(n->left);
cout<<n->data<<"\t";
inorder(n->right);
}
}
void bstree::preorder(node *n)const
{
if(n!=NULL)
{
cout<<n->data<<"\t";
preorder(n->left);
preorder(n->right);
}
}
void bstree::postorder(node *n)const
{
if(n!=NULL)
{
postorder(n->left);
postorder(n->right);
cout<<n->data<<"\t";
}
}
void bstree::del(int item)
{
node *parent;
if(root==NULL)
{
cout<<"\nTree empty\n";
getch();
return;
}
node *loc=search(item,parent);
if(loc==NULL)
{
cout<<"\nNode not found\n";
getch();
return;
}
node *ptr=NULL;
if(loc->left!=NULL && loc->right!=NULL)
{
ptr=loc->right;
parent=loc;
while(ptr->left!=NULL)
{
parent=ptr;
ptr=ptr->left;
}
loc->data=ptr->data;
loc=ptr;
}
if(loc->left==NULL)
ptr=loc->right;
else if(loc->right==NULL)
ptr=loc->left;
if(parent==NULL)
root=ptr;
else if(loc==parent->left)
parent->left=ptr;
else
parent->right=ptr;
delete loc;
cout<<"\nNode Deleted\n";
getch();
}
void bstree::inorder_destroy(node *r)
{
if(r!=NULL)
{
inorder_destroy(r->left);
del(r->data);
inorder_destroy(r->right);
}
}
int main()
{
bstree b;
int choice,flag=1,item;
while(flag)
{
clrscr();
cout<<"\n\t\t\t1-Insert";
cout<<"\n\t\t\t2-Delete";
cout<<"\n\t\t\t3-Traverse";
cout<<"\n\t\t\t4-Exit";
cout<<"\n\tEnter your choice:-";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nEnter the item to insert ";
cin>>item;
b.insert(item);
break;
case 2:
cout<<"\nEnter the item to delete ";
cin>>item;
b.del(item);
break;
case 3:
b.traverse();
break;
case 4:
flag=0;
break;
default:
continue;
}
}
getch();
return 0;
}

aniket.awati
18-03-2008, 01:11 PM
Hello friends,
I have devoloped a sudoku game using cpp. Please evaluate this
code. I have used Borland Turbo cpp 3.0 compiler and hence it is windows based and 16 bit.I know iI should shift to more advanced and open source devolopement enviornments, and I am going to do just that.
But it would be very nice of you to overlook this drawback and give me your suggestions on the logic in the code. I would also like to know about open source graphics libraries.

One more thing, initialy i wanted to code this thing using c language. But I couldn't use clock_t in 'c'.
Hence i changed it to 'cpp' just for that. As a result you may see printf and scanf used in 'cpp' coding.
Though it is not advisable, I can't help it as it is very time consuming to change it, now that the code has gone on to 800 lines.

you can get it here : http://www.esnips.com/web/aniket-programming

mehulved
18-03-2008, 02:05 PM
I would also like to know about open source graphics libraries.
Qt? svgalib?

aniket.awati
18-03-2008, 07:49 PM
Is that a graphic library? svgalib?

aditya.shevade
18-03-2008, 07:59 PM
^^ Yes it is a graphics library (and a very good one, as Mehul was kind enough to let me know about it).

@ Mehul, we can use Qt for drawing lines? You serious? And I read that Qt is not entirely free on Windows.... shade more light....

mehulved
18-03-2008, 08:52 PM
Qt is under dual license. GPL and commercial version. The license enforcement doesn't happening according to the platform but according to the nature of your project.
If your project is in open source then you can use the GPL'ed version of Qt, which is available free of cost on any supported platform.
If you're having a commercial project i.e. non-open source, even if it's free of cost, you have to used commercial version of Qt, which costs money. Sykpe and Opera are made using the paid version. The paid version is one license per user and not per computer. See http://trolltech.com/developer/downloads/qt/faq
Yeah, you do not get Qt binaries on windows. You will have to compile Qt on your own using mingw. See http://trolltech.com/developer/downloads/qt/windows
I don't exactly know the difference between graphic capabilities of different libraries out there, but you could check Qt documents for your info. Basically Qt is a GUI programming library for C++ that's as far as I know.

aditya.shevade
18-03-2008, 10:00 PM
^^ That is what I thought... Qt is a GUI development thing.... and aniket.awati wants something to draw lines and circles.....

baccilus
19-03-2008, 08:15 PM
Here's a program to calculate the computer sales man salary. Base salary, bonus rate and commission are fixed. Input of price per system and number of systems sold is from user.

#include<stdio.h>
#define BASE_SAL 2000.0
#define BONUS 200.0
#define COMMISSION 0.02

//Compiled on linux using geany

int main()
{
int n;
float bonus, commission, price;
float gross_sal;
printf("\nInput the number of systems sold ",n);
scanf("%d",&n);
printf("\nInput the price of each computer ",price);
scanf("%f",&price);
bonus=BONUS*n;
commission=COMMISSION*price*n;
gross_sal=BASE_SAL+bonus+commission;
printf("Base salary is %f\nBonus is %f\nCommission is %f\nGross salary is %f"
,BASE_SAL,commission,gross_sal);
}

Gross salary is coming out to be wrong but I don't know why. Please tell if you realize that.

QwertyManiac
19-03-2008, 08:46 PM
You missed a variable in your printf() statement. The bonus variable I suppose. Or BONUS. :?

#include<stdio.h>
#define BASE_SAL 2000.0
#define BONUS 200.0
#define COMMISSION 0.02

//Compiled on linux using geany

int main()
{
int n;
float bonus, commission, price;
float gross_sal;
printf("\nInput the number of systems sold ",n);
scanf("%d",&n);
printf("\nInput the price of each computer ",price);
scanf("%f",&price);
bonus=BONUS*n;
commission=COMMISSION*price*n;
gross_sal=BASE_SAL+bonus+commission;
printf("Base salary is %f\nBonus is %f\nCommission is %f\nGross salary is %f\n"
,BASE_SAL,bonus,commission,gross_sal);
}

baccilus
19-03-2008, 09:20 PM
Thanks. Got it !!

radonryder
20-03-2008, 05:10 PM
A lot of this stuff form my comp text..
I can prepare 4 my comp boards with digitXD
Well here's what I did for my school project
It basicaly an inventory tracker software...
I coded the entire thing myself..
I just custumised the menu's for a gunshop...
Im open to reviews.Tell me what you guys think of it....

>>>GUNSHOP<<<

#include<iostream.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
#include<fstream.h>
#include<conio.h>
class staff
{
char sname[20]; //name of employee
int age; //age of employee
float salary; //salary of employee
char job[20]; //type of job done(eg-cashier,delivery man)
public:

void inputs()
{
cout<<"\nEnter New employee's Name:\n";
gets(sname);
cout<<"\nEnter New employee's Age:\n";
cin>>age;
cout<<"\nEnter New employee's job:\n";
gets(job);
cout<<"\nEnter New employee's Salary:\n";
cin>>salary;
}
void displays()
{ cout<<"\n";
cout<<sname;
cout<<"\t\t"<<age;
cout<<"\t\t"<<job;
cout<<"\t\t"<<salary;
}
int searchst(char st1[20]) //search by name of employee
{
if(strcmp(st1,sname)==0)
return 0;
else
return 1;
}
//search by job of employee
int searchjs(char j1[20] )
{
if(strcmp(j1,job)==0)
return 0;
else
return 1;
}



};
class guns
{
char gname[20]; //name of gun
char gtype[20]; //type of gun(eg-pistol,shotgun,etc)
int stock;
float cost; //no: of guns in stock
public:

void inputg() //for inputing gun details
{
cout<<"\nEnter Gun Name:\n";
gets(gname);
cout<<"\nEnter Type of Gun:\n";
gets(gtype);
cout<<"\nEnter Gun's Price:\n";
cin>>cost;
cout<<"\nEnter the Number of Guns in stock:\n";
cin>>stock;
}

void displayg() //for displaying gun details
{ cout<<"\n";
cout<<gname;
cout<<"\t\t"<<gtype;
cout<<"\t\t"<<cost;
cout<<"\t\t"<<stock;
}
int rtstk()
{
return stock;
}

int searchgn(char gn1[20]) //search by gun name
{
if(strcmp(gn1,gname)==0)
return 0;
else
return 1;
}

int searchct(char ty1[20] ) //search by gun type
{
if(strcmp(ty1,gtype)==0)
return 0;
else
return 1;
}

//updated stock
void update(int i)
{
stock=stock+i;


} //updates cost
void update1()
{
float cos;
cout<<"\nEnter New price:\n";
cin>>cos;
cost=cos;
} //for purchasing
void update2(int i)
{
stock=stock-i;
}
};
guns g1;
staff s1;
void main()
{
char str[20],a;
fstream f1,f2,f3;
char ch1;
int n,ch,i,j,k,ch3;
char ch4;
cout<<"\n \n\n\nWelcome to Ammunation";
do
{ clrscr(); //start of 1st do while
cout<<"\nSelect access level";
cout<<"\n1.Staff";
cout<<"\n2.Admin";
cout<<"\n3.Exit\n";
cin>>ch;
if(ch==1)
{clrscr();
do //start of staff menu
{
cout<<"\n\nStaff Menu"; //gun management
cout<<"\n----------------------------------------";
cout<<"\n1.Display all Guns";
cout<<"\n2.Search by Gun Name";
cout<<"\n3.Search by Gun Type";
cout<<"\n4.Purchase Guns";
cout<<"\n5.Add more Guns";
cout<<"\n6.Update Gun details";
cout<<"\n7.Delete Guns";
cout<<"\n8.Restore deleted Guns";
cout<<"\n9.Create Backup";
cout<<"\n10.restore basckup(Caution!!->Use only if Backup File exits)";
cout<<"\n11.Clear Screen";
cout<<"\nEnter your choice\n";
cin>>ch3;
switch (ch3)
{
case 1:
f1.open("gun1.dat",ios::in);
f1.read((char*)&g1,sizeof(g1));
cout<<"\nGun details";
cout<<":\n--------------------------";
cout<<"\nName\t"<<"\tType\t"<<"\tPrice\t"<<"\tStock\t";
while(f1)
{
g1.displayg();
f1.read((char*)&g1,sizeof(g1));
}
f1.close();
break;
case 2:
cout<<"\nEnter the Name of the Gun:\n";
gets(str);
f1.open("gun1.dat",ios::in);
f1.read((char*)&g1,sizeof(g1));
while(f1)
{

n=g1.searchgn(str);
if(n==0)
{
cout<<"\nGun details";
cout<<":\n--------------------------";
cout<<"\nName\t"<<"\tType\t"<<"\tPrice\t"<<"\tStock\t";
g1.displayg();
break;
}
f1.read((char*)&g1,sizeof(g1));
}
if(n==1)
{
cout<<"\nThe Gun was Not Found";
}
f1.close();




break;

case 3: cout<<"\nEnter Type of Gun:\n";
gets(str);
f1.open("gun1.dat",ios::in);
f1.read((char*)&g1,sizeof(g1));
i=0;
while(f1)
{

n=g1.searchct(str);
if(n==0)
{i++;
if(i==1)
cout<<"\nGun details";
cout<<":\n--------------------------";
cout<<"\nName\t"<<"\tType\t"<<"\tPrice\t"<<"\tStock\t";
g1.displayg();
break;
}
f1.read((char*)&g1,sizeof(g1));
}
if(n==1)
{
cout<<"\nThere are no guns of "<<str<<" Type";
}
f1.close();
break;

case 4:
cout<<"\nEnter the Name of the Gun:\n";
gets(str);
f1.open("gun1.dat",ios::in|ios::out);
f1.read((char*)&g1,sizeof(g1));
k=sizeof(g1);
while(f1)
{
n=g1.searchgn(str);

if(n==0)
{
i=g1.rtstk();
cout<<"\nEnter the Number of Guns to be Purchased:\n";
cin>>j;
if(j>i)
{
cout<<"\nThe Number of guns Requested is Not in Stock";
cout<<"\nOnly "<<i<<" Are Available";
cout<<"\nDo u still wish to buy?:/n";
cin>>a;
if(a=='y'||a=='Y')
{f1.seekp(-k,ios::cur);
g1.update2(j);

f1.write((char*)&g1,sizeof(g1));
cout<<"\nPurchased ";
}
}
else
{ f1.seekp(-k,ios::cur);
g1.update2(j);

f1.write((char*)&g1,sizeof(g1));
cout<<"\nPurchased ";
}
break;
}
f1.read((char*)&g1,sizeof(g1));

}

if(n==1)
{
cout<<"\nGun not in stock";
}




f1.close();



break;
case 5:
f1.open("gun1.dat",ios::out|ios::app);
cout<<"\nEnter the no: of guns:\n";
cin>>n;
cout<<"\nEnter Gun Details:\n";
cout<<"---------------------------------";
for(i=0;i<n;i++)
{
g1.inputg();
f1.write((char*)&g1,sizeof(g1));
}

f1.close();
break;


case 6: cout<<"\nEnter the Name of the Gun:\n";
gets(str);
k=sizeof(g1);
f1.open("gun1.dat",ios::in|ios::out);
f1.read((char*)&g1,sizeof(g1));
while(f1)
{

n=g1.searchgn(str);
if(n==0)
{
g1.update1();
cout<<"\nEnter the no: of guns delivered:\n";
cin>>i;
f1.seekp(-k,ios::cur);
g1.update(i);

f1.write((char*)&g1,sizeof(g1));
cout<<"\nUpdated";
break;
}
f1.read((char*)&g1,sizeof(g1));
}
if(n==1)
{
cout<<"\nThe Gun was Not Found";
}
f1.close();


break;
case 7: f1.open("gun1.dat",ios::in);
f2.open("gun2.dat",ios::out|ios::app);
f3.open ("gun3.dat",ios::out|ios::app);
cout<<"\nEnter Name of the gun";
gets(str);
i=0;
f1.read((char*)&s1,sizeof(s1));
while(f1)
{
n=g1.searchgn(str);
if(n==0)
{i++;
f3.write((char*)&g1,sizeof(g1));
}
if(n==1)
{
f2.write((char*)&g1,sizeof(g1));
}
f1.read((char*)&g1,sizeof(g1));
}
f1.close();
f2.close();
f3.close();
remove("gun1.dat");
rename("gun2.dat","gun1.dat");
if(i==0)
cout<<"\nGun not found";
break;

case 8: f1.open("gun1.dat",ios::out|ios::app);
f2.open("gun3.dat",ios::in);
cout<<"\nEnter the Gun's Name:\n";
gets(str);
i=0;
while(f2)
{
n=g1.searchgn(str);
if(n==0)
{i++;
f1.write((char*)&g1,sizeof(g1));
}
f2.read((char*)&g1,sizeof(g1));
}
if(i==0)
cout<<"\nGun not found";
else
cout<<"\nGun Restored";
f1.close();
f2.close();

break;
case 9: f1.open("gun1.dat",ios::in);
f2.open("bkg.dat",ios::out|ios::app);
f1.read((char*)&g1,sizeof(g1));

while(f1)
{
f2.write((char*)&g1,sizeof(g1));
f1.read((char*)&g1,sizeof(g1));
}
f2.close();
f1.close();
cout<<"\nBackUp Created";
break;

case 10: remove("gun1.dat");
rename("bkg.dat","gun1.dat");
cout<<"\nBackup Restored";
break;
case 11:
clrscr();
break;
default:cout<<"\nWrong choice\n";

}
cout<<"\nDo u wish to continue?";
cin>>ch4;

}while(ch4=='y'||ch4=='Y');
//end of staff menu
}
if(ch==2)
{

clrscr();
do
{
cout<<"\n\nAdmin Menu"; //employee management
cout<<"\n----------------------------------------";
cout<<"\n1.Search by Name of employee";
cout<<"\n2.Display all Staff Details";
cout<<"\n3.Display staff in a specific Catagory ";
cout<<"\n4.Update employee Details";
cout<<"\n5.Hire Staff";
cout<<"\n6.Fire Staff";
cout<<"\n7.Fired Staff History";
cout<<"\n8.Rehire staff";
cout<<"\n9.Create Backup";
cout<<"\n10.restore basckup(Caution!!->Use only if Backup File exits)";
cout<<"\n11.Clear screen";
cout<<"\nEnter Choice?";
cin>>ch3;
switch(ch3)
{
case 1: f1.open("stf1.dat",ios::in);
f1.read((char*)&s1,sizeof(s1));
cout<<"\nEnter Name of the Employee:\n";
gets(str);
while(f1)
{
n=s1.searchst(str);
if(n==0)
{
cout<<"\nEmployee Details";
cout<<"\n-----------------------";
cout<<"\nName\t"<<"\tAge\t"<<"\tJob\t"<<"\tSalary\t";
s1.displays();
break;
}
f1.read((char*)&s1,sizeof(s1));
}
f1.close();
if(n==1)
cout<<"\nEmployee does Not Work here";
break;

case 2:
f1.open("stf1.dat",ios::in);
f1.read((char*)&s1,sizeof(s1));
cout<<"\nEmployee Details";
cout<<"\n-----------------------";
cout<<"\nName\t"<<"\tAge\t"<<"\tJob\t"<<"\tSalary\t";

while(f1)
{
s1.displays();
f1.read((char*)&s1,sizeof(s1));
}
f1.close();
break;

case 3: i=0;
f1.open("stf1.dat",ios::in);
f1.read((char*)&s1,sizeof(s1));
cout<<"\nPlease Enter job of the employee";
gets(str);
while(f1)
{
n=s1.searchjs(str);
if(n==0)
{i++;
if(i==1)
{
cout<<"\nEmployee Details";
cout<<"\n-----------------------";
cout<<"\nName\t"<<"\tAge\t"<<"\tJob\t"<<"\tSalary\t";
}
s1.displays();
}
f1.read((char*)&s1,sizeof(s1));
}
f1.close();
if(i==0)
break;
case 4:f1.open("stf1.dat",ios::out|ios::in);
cout<<"\nEnter the Employee's Name:\n";
gets(str);
i=sizeof(s1);
f1.read((char*)&s1,sizeof(s1));
while(f1)
{
n=s1.searchst(str);
if(n==0)
{
f1.seekg(-i,ios::cur);
cout<<"\nEnter New Details";
cout<<":\n--------------------------";
s1.inputs();
f1.write((char*)&s1,sizeof(s1));
break;
}
f1.read((char*)&s1,sizeof(s1));
}
if(n==1)
cout<<"\nEmployee does Not work here";
f1.close();
break;
case 5: f1.open("stf1.dat",ios::out|ios::app);
cout<<"\nEnter the no: New employees:\n";
cin>>n;
cout<<"\nEnter New employee details";
cout<<":\n--------------------------";
for(i=0;i<n;i++)
{
s1.inputs();
f1.write((char*)&s1,sizeof(s1));
}

f1.close();
break;

case 6:f1.open("stf1.dat",ios::in);
f2.open("stf2.dat",ios::out|ios::app);
f3.open ("stf3.dat",ios::out|ios::app);
cout<<"\nEnter Name of the Employee:\n";
gets(str);
i=0;
f1.read((char*)&s1,sizeof(s1));
while(f1)
{
n=s1.searchst(str);
if(n==0)
{i++;
f3.write((char*)&s1,sizeof(s1));
}
if(n==1)
{
f2.write((char*)&s1,sizeof(s1));
}
f1.read((char*)&s1,sizeof(s1));
}
f1.close();
f2.close();
f3.close();
remove("stf1.dat");
rename("stf2.dat","stf1.dat");
if(i==0)
cout<<"\nEmployee does Not Work here";
break;


case 7:
f1.open("stf3.dat",ios::in);
f1.read((char*)&s1,sizeof(s1));
cout<<"\nFired Employee Histiry";
cout<<"\n-----------------------";
cout<<"\nName\t"<<"\tAge\t"<<"\tJob\t"<<"\tSalary\t";

while(f1)
{
s1.displays();
f1.read((char*)&s1,sizeof(s1));
}
f1.close();
break;
case 8:f1.open("stf1.dat",ios::out|ios::app);
f2.open("stf3.dat",ios::in);
cout<<"\nEnter the Employee's Name:\n";
gets(str);
i=0;
while(f2)
{
n=s1.searchst(str);
if(n==0)
{i++;
f1.write((char*)&s1,sizeof(s1));
}
f2.read((char*)&s1,sizeof(s1));
}
if(i==0)
cout<<"\nName not found";
else
cout<<"\nEmployee rehired";
f1.close();
f2.close();
break;
case 9: f1.open("stf1.dat",ios::in);
f2.open("bks.dat",ios::out|ios::app);
f1.read((char*)&s1,sizeof(s1));

while(f1)
{
f2.write((char*)&s1,sizeof(s1));
f1.read((char*)&s1,sizeof(s1));
}
f2.close();
f1.close();
cout<<"\nBackUp Created";
break;

case 10: remove("stf1.dat");
rename("bks.dat","stf1.dat");
cout<<"\nBackup Restored";
break;
case 11:

clrscr();
break;
default:
cout<<"\nWrong Choice";

}
cout<<"\nDo you wish to continue?";
cin>>ch4;
}while(ch4=='y'||ch4=='Y');
}
if(ch==3)
{

clrscr();
exit(0);
}
else
{
cout<<"\nWrong choice";

cout<<"\nDo u wish to select another access level";
cin>>ch1;

}
}while(ch1=='y'|ch1=='Y');

cout<<"\n\n\n\n";
clrscr();

} //end of program

deviprasad742
27-03-2008, 03:21 PM
Code For Sudoku:

Here is the code to solve Sudoku which i wrote in C

It's bit-lenghty but it can solve all difficulty levels

here is the input format to be given at the command prompt
--->./a.out

11 5
12 4
14 6
15 1
19 8
23 1
25 9
29 4
31 1
33 5
37 7
40 9
44 2
47 6
49 8
51 3
55 2
61 7
66 5
68 3
69 6
-1

0-80 is the grid numbers

the format should be [grid number(0-80) [1-9]

input should end with -1;



Here is the source code:


#include<stdio.h>
#include<stdlib.h>

int check,zcount;


int update(int pos);
int re_update(int pos);


struct str
{
int num;
int flag[10];
int group[20];
};



int count[81],once=0;

typedef struct str mynode;