Post your C/C++ Programs Here

Status
Not open for further replies.

The Conqueror

Elevating Humanity
look, there will be 2 questions. one will be easy and you will be easily able to create an algo for it.
the other one is usually tough and you will need to apply some popular algo. it will seem difficult as we r not taught these algos in schools. it usually gives a problem where knapsack and dynamic programming is applicable.
only those who solve both problems are selected(as i wasnt selected in two ZCOs where i had solved only one problem as i wasnt aware of knapsack algo).

the MIT OCW has a good lecture on knapsack problems and application of dynamic programming on such probs.
also there are many tutorials for it.

i also suggest you to learn "dijkstra algorithm"(for shortest path problems) from somewhere(i cant find the page from which i learnt it, so google it)
So If I learn those basic algorithms and data structures in say- in the month of May. My basics about arrays,pointers etc are perfect. Can I expect something good that I will be able to attempt it well at ZCO?
 

Neuron

Electronic.
@mohityadavx : Dude sorry to say but your program contain plenty of errors,like you are redefining the library functions like sin ,cos etc.It appears like you are low on the basics.Start learning from a good text book.I'd recommend C++ ,The Complete Reference by Herbert Schildt.It contains everything you need to know.Either buy one or search your school/college library.Good Luck :smile:
 

mohityadavx

Youngling
@mohityadavx : Dude sorry to say but your program contain plenty of errors,like you are redefining the library functions like sin ,cos etc.It appears like you are low on the basics.Start learning from a good text book.I'd recommend C++ ,The Complete Reference by Herbert Schildt.It contains everything you need to know.Either buy one or search your school/college library.Good Luck :smile:

ya u r right about the bugs

its not like that neuron this program would be now modified for complex function by my friend ( i am just preparing the basic outline) like square(sinx) + sin( square x) and i don't want to clutter main()

what about password bug
 

Neuron

Electronic.
Like arpanmukherjee1 said you will have to add the pass='\0' statement and it should work perfectly.
 

Neuron

Electronic.
@Liverpool_fan:'\r' works fine.

like i said before its still not working
It must, because that block of code is faultless and is running perfectly on my compiler.
Did you try running that piece of code alone?What error did you get? or why are you saying that it's not working?
 

Neuron

Electronic.
Run this and post your output.
Code:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<math.h>

void main()
{
char pass[10];
int i=0;
cout<<"Enter password"<<endl;
while((pass[i]=getch())!='\r')
{
cout<<"*";
i++;
}
pass[i]='\0';
cout<<endl;
if(strcmp(pass,"mohit")==0)
cout<<"Password Comparison Successfull"<<endl;
else
cout<<"Failed Password is "<<pass<<endl;

getch();
}
 

mohityadavx

Youngling
Run this and post your output.
Code:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<math.h>

void main()
{
char pass[10];
int i=0;
cout<<"Enter password"<<endl;
while((pass[i]=getch())!='\r')
{
cout<<"*";
i++;
}
pass[i]='\0';
cout<<endl;
if(strcmp(pass,"mohit")==0)
cout<<"Password Comparison Successfull"<<endl;
else
cout<<"Failed Password is "<<pass<<endl;

getch();
}

thanx neuron i am extremely sorry for my stupidity but i had caps turned on while typing the password and didn't noticed it.

anyway can u tell me something why doesn't string take the last character as null by default it does s when we use cin command etc
 

Liverpool_fan

Sami Hyypiä, LFC legend
Why would it take the last character as '\0' when you are coding the input manually character by character using getch?

BTW be informed that the code you have written is largely out of date, non-portable and does not conform to ANSI standards. If you are studying C++ independently, you are advised to learn ANSI C++ and use a modern compiler rather than the antiquated junk Turbo C++.
 
^^^+1. could have said it better myself.

@mohityadavx use GCC or DevC++. majority of the errors and warnings will be pointed by the compiler.

also treat warnings as errors, that is a good practice
 

mohityadavx

Youngling
@mohityadavx : Dude sorry to say but your program contain plenty of errors,like you are redefining the library functions like sin ,cos etc.It appears like you are low on the basics.Start learning from a good text book.I'd recommend C++ ,The Complete Reference by Herbert Schildt.It contains everything you need to know.Either buy one or search your school/college library.Good Luck :smile:

Thanx for the advice never knew sumita arora was that bad that most of you are simply rejecting the code !!!! I will use Dev C++ as i like its layout more then GCC!!!
 

nims11

BIOS Terminator
Thanx for the advice never knew sumita arora was that bad that most of you are simply rejecting the code !!!! I will use Dev C++ as i like its layout more then GCC!!!

sumita arora is not recommended for anyone who wants to do programming. :D

btw Dev-C++ is an IDE and GCC is a compiler. :)
 

nims11

BIOS Terminator
typing test, pretty lame, but i like it! :)

Code:
//TYPING TEST
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <fstream.h>
#include <string.h>
using namespace std;
void fileChk(char filename[10])
{
    ifstream fin(filename);
    if(!fin.is_open())
    {
       ofstream fout("scores.dat");
       fout<<0<<endl;
       fout.close();
    }
    fin.close();
}
void writeScores(double score)
{
       fileChk("scores.dat");
       ifstream fin("scores.dat");
     int n,i;
     char names[10][20];
     double scores[10];
     fin>>n;
     for(i=0;i<n;i++)
     {
                     fin>>names[i];
                     fin>>scores[i];
     }
     if(n==0)
     {
             n=1;
             fin.close();
     remove("scores.dat");
     ofstream fout("scores.dat");
     fout<<1;
     cout<<"\nCongratulations!!!, You made it to the HALL OF FAME\n\n";
         char name[20];
         cout<<"Enter you name - ";
         cin>>name;
     fout<<name<<" "<<score<<endl;
     }else{
     if(n<10)
     n++;
     fin.close();
     remove("scores.dat");
     ofstream fout("scores.dat");
     fout<<n<<endl;
     int flag=1;
     for(i=0;i<n;i++)
     {
       if((scores[i]>score || (i==n-1 && scores[n-1]>score)) && flag==1)
       {
         flag=0;
         n=n-1;
         cout<<"\nCongratulations!!!, You made it to the HALL OF FAME\n\n";
         char name[20];
         cout<<"Enter you name - ";
         cin>>name;
         fout<<name<<" "<<score<<endl;
       }
       fout<<names[i]<<" "<<scores[i]<<endl;
     }
     fout.close();
     }
}
void dispScores()
{
     system("cls");
     ifstream fin("scores.dat");
     int n,i;
     fin>>n;
     cout<<"name \ttime\n\n";
     for(i=0;i<n;i++)
     {
                     char name[20];
                     fin>>name;
                     double score;
                     fin>>score;
                     cout<<name<<"\t"<<score<<endl;
     }
}
int main()
{
  cout<<"TYPING SPEED TESTER BY NIMS13\nPress any key to continue....";
  getch();
  system("cls");
  char c;
  clock_t start, end;
  char str[100]="the quick brown fox jumps over the lazy dog";
  int len=strlen(str),i,errors=0;
  cout<<"Hello!! ,Type the following as fast as you can\n(The time will start when you type the first letter and will automatically end when you complete the sentence.\nDON'T PRESS ENTER WHEN YOU FINISH WRITING) -\n\nthe quick brown fox jumps over the lazy dog\n\n";
  int flag=1;
  while(flag)
  {
  c=getch();
  if(tolower(c)==str[0])
  {
                        cout<<c;
                        flag=0;
  start = clock();
}else
errors++;
}
  for(i=1;i<len;i++)
  {
     int flag=1;
     while(flag)
     {
     c=getch();
     if(tolower(c)==str[i])
     {
     cout<<c;
     flag=0;
     }else
errors++;
     }
  }
  end = clock();
  double duration=(double(end-start))/((double)CLOCKS_PER_SEC);
  system("cls");
  for(i=0;i<10;i++)
  {
     cout<<"Processing Data";
     cout<<"/";
     for(int j=0;j<10000000;j++);
     system("cls");
     cout<<"Processing Data";
     cout<<"-";
    for(int j=0;j<10000000;j++);
    system("cls");
    cout<<"Processing Data";
     cout<<"\\";
     for(int j=0;j<10000000;j++);
     system("cls");
     cout<<"Processing Data";
     cout<<"|";
     for(int j=0;j<10000000;j++);
     system("cls");
  }
  cout<<"\nTotal Time - "<<duration<<" secs!!\n";
  cout<<"\nMistakes made- "<<errors;
  for(long j=0;j<100000000;j++);
  cout<<"\nPress Any Key to See the Hall of Fame....";
  getch();
  system("cls");
  writeScores(duration);
  dispScores();
  cin>>i;
  return 0;
}

compiled exe
 

Neuron

Electronic.
^^ lol,nice program.

Finally learning some Windows programming.This one is nothing.Just a line drawing program.
But you guys can expect some games from me pretty soon :mrgreen:.

Code:
#include<windows.h>
#include<string>
#include<vector>

using namespace std;



HWND hwnd;
MSG msg;
WNDCLASSEX wndclass;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM );
PAINTSTRUCT ps;
HDC hdc;


int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{

	LOGBRUSH lb;
	lb.lbStyle=BS_SOLID;
	lb.lbColor=RGB(125,123,123);
	HBRUSH BlueBrush=CreateBrushIndirect(&lb);

	wndclass.cbClsExtra=0;
	wndclass.cbSize=sizeof(wndclass);
	wndclass.cbWndExtra=0;
	wndclass.hbrBackground=(HBRUSH)BlueBrush;
	wndclass.hCursor=LoadCursor(0,IDC_ARROW);
	wndclass.hIcon=LoadIcon(0,IDI_APPLICATION);
	wndclass.hIconSm=LoadIcon(0,IDI_APPLICATION);
	wndclass.hInstance=hInstance;
	wndclass.lpfnWndProc=WndProc;
	wndclass.lpszClassName="Wind";
	wndclass.lpszMenuName=0;
	wndclass.style=CS_HREDRAW|CS_VREDRAW;
	

	RegisterClassEx(&wndclass);


	hwnd=CreateWindow("Wind","Windo",WS_HSCROLL|WS_VSCROLL,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

	ShowWindow(hwnd,nShowCmd);
	UpdateWindow(hwnd);

	ZeroMemory(&msg,sizeof(msg));

	while(GetMessage(&msg,0,0,0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

}




struct tLine {
	POINT P0;
	POINT P1;
};

vector<tLine> aLines;
tLine tLines;
bool mousedown=true;





LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM Wparam,LPARAM Lparam) 
{
	switch(msg) {

		case WM_LBUTTONDOWN:
			tLines.P0.x=LOWORD(Lparam);
			tLines.P0.y=HIWORD(Lparam);
			SetCapture(hwnd);
			mousedown = true;			
			return 0;
		
		case WM_LBUTTONUP:
			ReleaseCapture();
			mousedown=false;
			tLines.P1.x=LOWORD(Lparam);
			tLines.P1.y=HIWORD(Lparam);
			aLines.push_back(tLines);
			return 0;

		case WM_MOUSEMOVE:
			if(mousedown) {			
				InvalidateRect(hwnd, 0, true);
				hdc=BeginPaint(hwnd,&ps);
				MoveToEx(hdc,tLines.P0.x,tLines.P0.y,0);
				LineTo(hdc,LOWORD(Lparam),HIWORD(Lparam));			
				EndPaint(hwnd,&ps);			
			}
				
			for(int i=0;i<aLines.size();i++) {
				InvalidateRect(hwnd, 0, false);
				hdc=BeginPaint(hwnd,&ps);
				MoveToEx(hdc,aLines[i].P0.x,aLines[i].P0.y,0);
				LineTo(hdc,aLines[i].P1.x,aLines[i].P1.y);
				EndPaint(hwnd,&ps);								
			}			
			return 0;

		case WM_RBUTTONDOWN:
			hdc=BeginPaint(hwnd,&ps);
			InvalidateRect(hwnd,0,true);
			aLines.clear();
			EndPaint(hwnd,&ps);
			return 0;

		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
		case WM_KEYDOWN:
			if(Wparam==VK_ESCAPE) {
				if(MessageBox(0,"Are you sure you want to exit?","Message",MB_YESNO)==IDYES){
					PostQuitMessage(0);
					return 0;
				}
			}
			break;
	}
	return DefWindowProc(hwnd,msg,Wparam,Lparam);

}
 

Neuron

Electronic.
Here is an updated version of my game which I posted earlier.Fixed some bugs.This is the compiled version.
You can get the code from post #635 .

Resumable Link.
DarkPong1.1

Rapidshare.
DarkPong1.1
 
Last edited:
Status
Not open for further replies.
Top Bottom