Post your C/C++ Programs Here

Status
Not open for further replies.

Sykora

I see right through you.
Re: Post ur C/C++ Programs Here

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...
 

arun_cool

Broken In
Re: Post ur C/C++ Programs Here

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

Pee into the Wind...
Re: Post ur C/C++ Programs Here

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*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.??

Code:
#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;
}
 
Last edited:

QwertyManiac

Commander in Chief
Re: Post ur C/C++ Programs Here

Code:
#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([B]sub==0[/B])
    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.

Code:
#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:
Code:
#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;
}
 
Last edited:

shady_inc

Pee into the Wind...
Re: Post ur C/C++ Programs Here

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

Beware of the innocent
Re: Post ur C/C++ Programs Here

something like this will work?
Code:
#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

Right off the assembly line
Re: Post ur C/C++ Programs Here

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,
Code:
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

Techie By Heart
Re: Post ur C/C++ Programs Here

function 2 input a matrix:-
Code:
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

Techie By Heart
Re: Post ur C/C++ Programs Here

Code:
/* 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

Commander in Chief
Re: Post ur C/C++ Programs Here

Code:
/* 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)

Code:
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
 

grvpuri

Right off the assembly line
Re: Post ur C/C++ Programs Here

//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

I am the Legend.........
Re: Post ur C/C++ Programs Here

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 : *www.esnips.com/web/aniket-programming
 
Last edited:

aditya.shevade

Console Junkie
Re: Post ur C/C++ Programs Here

^^ 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 Till I Die............
Re: Post ur C/C++ Programs Here

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 *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 *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.
 
Status
Not open for further replies.
Top Bottom