Post your C/C++ Programs Here

Status
Not open for further replies.

QwertyManiac

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

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

#define PI 3.1416

float radius(float d) { return d/2; }
float surface(float d) { return 4*PI*pow(d/2,2); }
float circum(float d) { return PI*d; }
float volume(float d) { return (4*PI*pow(d/2,3))/3; }

int main(int argc, char **argv)
{
    float dia;

    printf("Enter the diameter: ");
    scanf("%f",&dia);
    dia<0.0?exit(1):printf("\nDiameter: %f", dia);

    printf("\nRadius is: %f", radius(dia));
    printf("\nSurface Area is: %f", surface(dia));
    printf("\nCircumference is: %f", circum(dia));
    printf("\nVolume is: %f\n", volume(dia));

    return 0;
}

Resulting I/O:
Code:
Enter the diameter: 4

Radius is: 2.000000
Surface Area is: 50.265598
Circumference is: 12.566400
Volume is: 33.510399

There, homework done. :\
 
Last edited:

sameissa

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

thank you very much

thank you very much
 
Last edited:

mavihs

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

Code:
/*WAP to copy content of one file to another
i) As it is.
ii) After converting all characters to upper case

Name: Shivam*/

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<stdlib.h>

void main()
{
    clrscr();
    char path1[100], path2[100], ch, ch2;
    int cho;
    cout<<"\n\tEnter the path of the file which has to be copied: ";
    gets (path1);
    ifstream org(path1);
    if(!org)
    {
        cout<<"\tFile not found!";
        exit(0);
    }
    cout<<"Enter the name or the path of the file where the file has to be copied to: ";
    gets(path2);
    ofstream wrt(path2);
    if(!wrt)
    {
        cout<<"\tCannot create file!";
        exit(0);
    }

    cout<<"\t 1. Enter 1 to copy file as it is. \n\t 2. Enter 2 to copy file after converting all lower case characters to upper case characters. \n\n\t Enter your choice: ";
    cin>>cho;
    if(cho==1)
    {
        do{
            org.get(ch);
            wrt.put(ch);
        }while(!org.eof());
    }
    else
    {
        if(cho==2)
        {
            do{
                org.get(ch);
                if(ch > 96 && ch <123)
                {
                    ch2 = ch - 32;
                    wrt.put(ch2);
                }
                else
                wrt.put(ch);
                
            }while(!org.eof());
        }
        else
        cout<<"Wrong choice entered!";
    }
}
 

gopi_vbboy

Cyborg Agent
Re: Post ur C/C++ Programs Here

Embedded programming is very interesting...Let me introduce to this type of programmign woth ANSI C

Here how a general super loop is written for any microcontroller--Superloop is just a while loop similar to a MessageProc in WIn32 API where u can wait for an interrupt or some message to arrive.......

Code:
*/
THe following is a sample prog for keil

This is how u can turn on a LED connect to a port of Microcontroller
say P0.0

#include <reg52.h> //i use keil...reg52 is the lib for atmel 89c52 series

void main()
{

while(1)
{
//super loop

P0.0=0;//MAKING LOW DRIVES CURRENT INTO LED
}
}
///Thsts it a simple LED turn onm prog...............actually a simple controller can also be use ///////but i jus took this for an example for beginner

Now jus program the.hex produced by compliler using some flash programming software.............done a embedded project
 

aditya.shevade

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

^^ LOL... Anyways, nice to see people going to embedded C... Make a thread for this, embedded C for microcontrollers.. name each post by the microcontroller for which the program is.

Try AVR controllers, they are nice.
 

umeshtangnu

NULL_PTR
Re: Post ur C/C++ Programs Here

Code:
unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count)
{
	int i =0;

	for(;i<count;i++)
		*(dest+i)=*(src+i);

	return  dest;
}

unsigned char *memset(unsigned char *dest, unsigned char val, int count)
{

	int i =0;
	while(i<count)
	{
		*(dest+i)=val;
		i++;
	}
	return  dest;
}

unsigned short *memsetw(unsigned short *dest, unsigned short val, int count)
{

	int i =0;
	while(i<count)
	{
		*(dest+i)=val;
		i++;
	}
	return  dest;
}

some common C functions
currently writing print(char * c) and printch(char c)
 

Plasma_Snake

Indidiot
Re: Post ur C/C++ Programs Here

Brethren can you please look at my this program, its my first try at Windows programming, copied from "Sams Teach Yourself Game Programming in 24 Hours" but still erroneous. Below is the download link to the rar archive containing the actual VS2005 Project. Please see it.
Code:
*www.uploading.com/files/JSXD03HX/Skeleton.rar.html
 

Plasma_Snake

Indidiot
Re: Post ur C/C++ Programs Here

Well since no one has actually taken the pain to see my project's code, can anybody at least tell me that while generating following error, where is the compiler actually looking for .ico files?
Code:
1>Skeleton.cpp
1>Compiling resources...
1>.\Skeleton.rc(4) : error RC2135 : file not found: Skeleton.ico
1>.\Skeleton.rc(5) : error RC2135 : file not found: Skeleton_sm.ico
1>Build log was saved at "file://c:\Users\Rudra Pratap\Documents\Visual Studio 2005\Projects\Skeleton\Skeleton\Debug\BuildLog.htm"
1>Skeleton - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
 

QwertyManiac

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

I can't download your source from that silly site. Its much like Megaupload, "The slots of your country is over-limit. (Actually, we dropped our scalable balls)". Haven't you heard of Dropbox, or Mediafire, or any other sane upload service?

But anyway, look into your Skeleton.rc file for the location its looking up or pastebin that file here if you don't understand. :)
 

srinivasa.s

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

This is something I never knew and read about it recently in "Exceptional C++". Let me start with a code example...

namespace A {
class S { };
void foo(S& parm) { }
}

namespace B {
void foo(A::S& parm) { }
void bar(A::S& parm) {
foo(parm); // Which "foo" does this call?
}
}


Try this out and to your surprise you'll find that this gives a "call to foo is ambiguous" error. But why?? The only foo visible at the place of call is in namespace B. Why should this result in an error? Okay, now try this out - comment out the "foo" definition in namespace B.

namespace A {
class S { };
void foo(S& parm) { }
}

namespace B {
// void foo(A::S& parm) { }
void bar(A::S& parm) {
foo(parm); // Which "foo" does this call?
}
}


This compiles without errors! Surprised again? When you run the program, it'd have called A::foo. What happened here? Does this not appear to be a namespace violation. Apparently not! This strange behavior is explained by "Koenig lookup".

If you supply a function argument of class type (here parm, of type A::S), then to find the function name the compiler considers matching names in the namespace (here A) containing the argument's type.

But still, is'nt this a namespace violation?? NO. Just consider this piece of code...

std::string str("Hello");
std::cout << str ;


This works fine right? How? This works because of Koenig lookup. The function in question here is "operator <<" which is found in "namespace std". But we have'nt used any "using" declaration to bring that function into the current scope. Since std::string is going as an argument into that function, the compiler will automatically look into the std namespace and will find that function. Without this kind of lookup, the same function call would have to be written as...

std::eek:perator <<( std::cout, str) ; // ugly! ain't it??

This is the great value that Koenig lookup gets us and we tend to use it every now and then without realising it.
 

jax_diu

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

can any one give me a simple programe of palindrome.....
pls people give it urgently......
 

srinivasa.s

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

I once needed to write a program that would modify a file based on certain parameters like: percentage of change, scatter changes over different extents, maximum length of a change extent, randomize changes, etc. To be efficient, the program needed to generate a random list of change extents, sort it and then iterate over the sorted list to seek to particular offset and change the file for the length of that extent.

An extent was a structure having a pair - {offset, length}. The sort needed to sort the list of extents based on the offsets. Instead of generating a list first and then sorting it, I chose to do a sorted insert into a linked list. One thing that needed to be taken care was to ensure no overlaps in the extents in the list.

Following is the function that implements the sorted insert with no overlaps:

Code:
struct node {
  unsigned pos;  // offset
  unsigned extent;  // extent length
  struct node *next; 
}; 

//
// return false if there's no insert due to overlap
// 
bool sorted_insert_no_overlap( node **head, node *new_node )
{
  node dummy;
  node *cur = &dummy;
  dummy.next = *head;
  while( cur->next && cur->next->pos < new_node->pos )
  {
    cur = cur->next;
  }
  if( ((cur->pos + cur->extent) > new_node->pos) || 
    ( cur->next
      && ( (cur->next->pos == new_node->pos)
            || ((new_node->pos + new_node->extent) > cur->next->pos) )
    ))
  {
    return false;
  }
  new_node->next = cur->next;
  cur->next = new_node;
  *head = dummy.next;
  return true;
}
 

nvidia

-----ATi-----
Re: Post ur C/C++ Programs Here

can any one give me a simple programe of palindrome.....
pls people give it urgently......
Here -
Code:
void main()
{
int number,rev,digit,temp;
printf("Enter the number");
scanf("%d",&number);
temp = number;
rev = 0;
while(temp!=0)
{ 
   digit = temp % 10;
   rev = rev*10 + digit;
   temp = temp /10;

}
if(number==rev)
printf("Number is a palindrome");
}
 
OP
Gigacore

Gigacore

Dreamweaver
Re: Post ur C/C++ Programs Here

can any one give me a simple programe of palindrome.....
pls people give it urgently......

here's an another simple method which also says the word is not a palindrome in case if it isn't:

Code:
#include <stdio.h>
void main()
{
     int i, length, flag=0;
     char string[25];
     printf("\nEnter the string: ");
     gets(string);
     length = strlen(string);
     for(i=0;i<length/2;i++)
     {
        if(string[i]!=string[--length])
        {
            flag =1;
        break;
        }
     }
  if (flag==1)
  printf("\nThe word is not a palindrome");
  else
  printf("\nThe word is a palindrome");
  getch();
}

@ nvidia, u missed the header btw...
 
Last edited:

Plasma_Snake

Indidiot
Re: Post ur C/C++ Programs Here

OK brothers, here is the MediaFire link to my project's rar file. This time I even made 2 icon resources but now getting an eof error in aresource1.h file which is automatically created when I build the project.
Code:
*www.mediafire.com/?1cyi22xq0jh
Please see to it and tell me what I'm doing wrong :?: :(
 

4T7

Journeyman
Re: Post ur C/C++ Programs Here

Here's mine
Code:
//Man with umbrella
//man_ubr.c
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
#include<conio.h>
void main()
{
  int driver=DETECT,mode,i;
  int x1,x2,x3,x4,x5;
  int y1,y2,y3,y4,y5;
  initgraph(&driver,&mode,"C:\\tc\\bgi");
  cleardevice();
  setcolor(9994);
  x1=100;y1=330;
  x2=150;y2=330;
  x3=125;y3=280;
  x4=0;y4=332;
  x5=600;y5=332;
  for(i=0;i<300;i+=10)
  for(x1=100,x2=150;x1<150,x2>100;x1+=2,x2-=2)
  {
	 delay(15);
	 cleardevice();
	 line(x3+i,240,150+i,240);
	 line(x3+i,240,x1+i,270);
	 line(150+i,240,150+i,180);
	 ellipse(150+i,185,0,200,35,5);
	 line(x1+i,y1,x3+i,y3);
	 line(x2+i,y2,x3+i,y3);
	 ellipse(125+i,260,0,360,5,25);
	 circle(125+i,225,7);
	 line(x4,y4,x5,y5);
  }
  return;
}
 

aditya.shevade

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

Okay, I have not seen your code, but I think (underline think) that the problem is due to a newline at the end of the file aresource1.h. Error is because there is no newline at the end of the file. Put a newline at the end (blank) of the file and save it. (Again, I think that is the problem, I have not seen the code)
 
Status
Not open for further replies.
Top Bottom