Post your C/C++ Programs Here

Status
Not open for further replies.

Liverpool_fan

Sami Hyypiä, LFC legend
Re: Post ur C/C++ Programs Here

^ ^ ^
Strange :?
That didn't happen to me. Did you try to do the complete install? Because you don't need to. You just need Base Tools and g++ (for C++) and possibly gdb but that's not necessary too.
Maybe it was a server problem. I suggest to try again, it doesn't take as long as waiting for the night... :)
If the problem crops up again, could you post a screenshot?

Also you can try installing the Candidate or previous version insted of the current version.

Alternatively you can try Codeblocks + MinGW integrated installer.
*www.codeblocks.org/downloads/5

(download the second installer of ~19MB which can integrated gcc and gdb)

However the downside of Codeblocks is that it's heavy and has WAY too many features that it borders on being confusing.

However the benefit would be that it wil automatically install MinGW and you can still use Geany as IDE.
 
Last edited:

Crazykiller

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

You wanted a C++ Program?? Here it is.


#include <iostream>
using namespace std;
void main()
{
cout << "Hello World!" << endl; cout << "Welcome to C++ Programming" << endl; }

I Made it myself. Really...............................
 
Re: Post ur C/C++ Programs Here

^ ^ ^
Strange :?
That didn't happen to me. Did you try to do the complete install? Because you don't need to. You just need Base Tools and g++ (for C++) and possibly gdb but that's not necessary too.
Maybe it was a server problem. I suggest to try again, it doesn't take as long as waiting for the night... :)
If the problem crops up again, could you post a screenshot?

Also you can try installing the Candidate or previous version insted of the current version.

Alternatively you can try Codeblocks + MinGW integrated installer.
*www.codeblocks.org/downloads/5

(download the second installer of ~19MB which can integrated gcc and gdb)

However the downside of Codeblocks is that it's heavy and has WAY too many features that it borders on being confusing.

However the benefit would be that it wil automatically install MinGW and you can still use Geany as IDE.
Heres the error:
*img10.imageshack.us/img10/9715/24342726.th.jpg

Here's the "Download Complete" message:
*img14.imageshack.us/img14/9985/56843628.th.jpg

Anyway, I've set it for download now. Lets see what happens...
 
OP
Gigacore

Gigacore

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

Hi guys,

I am playing with a program to concatenate two string and find the length of the string using pointers. I am able to concatenate two strings, but when I try to find the length of the string, it gives incorrect output.

for example, if the given string is "forum", the output displays the length of the string as 1 instead of 5...

Here's the code:

Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void menu();
void CONCAT(char *, char *);
void main()
{
	char str1[30], str2[30];
	int choice;

	while(1)
	{
		menu();
		scanf("%d", &choice);
		fflush(stdin);
		switch (choice)
		{
			case 1: printf("\nConcatenation operation\n");
				printf("\nEnter first string");
				gets(str1);
				fflush(stdin);
				printf("\nEnter second string");
				gets(str2);
				fflush(stdin);
				CONCAT(str1, str2);
				printf("\Concatenated string is \n");
				puts(str1);
				break;
			case 2: printf("\nLength operation \n");
				printf("\nEnter the string");
				gets(str1);
				printf("\nLength %d",LENGTH(str1));
				break;
			case 3: exit(0);
		}
		getch();
	}
}
void menu()
{
clrscr();
printf("\n1. Contenation of two strings");
printf("\n2. Length of the string");
printf("\n3. Exit");
printf("\nEnter your choice ");
}
void CONCAT(char *ptr1, char *ptr2)
{
	while(*ptr1!='\0') ++ptr1;
	while(*ptr2!='\0')
	{
		*ptr1=*ptr2;
		++ptr1;
		++ptr2;
	}
	*ptr1='\0';
	return;
}
int LENGTH(char *Ptr)
{
int l = 0;
while(*Ptr!='\0')
	{
		++l;
		++Ptr;
	}
return 1;
}

what's the error?
 

Liverpool_fan

Sami Hyypiä, LFC legend
Re: Post ur C/C++ Programs Here

Code:
int LENGTH(char *Ptr)
{
int l = 0;
while(*Ptr!='\0')
	{
		++l;
		++Ptr;
	}
[B]return 1[/B];
}
Why are you returning *ONE* here? This should be return l (smallcase L)
 
OP
Gigacore

Gigacore

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

oh sh*t.. return 1 and 0 has become a practice for me.. :p

Thanks Liverpool :)
 

melody~

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

some inputs from ma side ... here's a 1 line prog to fine if da number is in da power of 2

#define is_power_of_two(n) (! n & (n-1))
 

vamsi360

Always confused
Re: Post ur C/C++ Programs Here

My OS Lab programs all written by me...I loved writing those....I rocked the lab!...
BANKER'S ALGORITHM
Code:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
int main() {
    int **max,**need,**alloc;
    int *avail,*flag,*sum,*res,c=0,count=0;
    int i,j,k,l,m,n;
    printf("\nEnter the dimensions of matrix:");
    scanf("%d %d",&m,&n);
    flag=(int *)malloc(m*sizeof(int));
    max=(int **)malloc(m * sizeof(int *));
    need=(int **)malloc(m * sizeof(int *));
    alloc=(int **)malloc(m * sizeof(int *));
    avail=(int *)malloc(n*sizeof(int));
    sum=(int *)malloc(n * sizeof(int));
    res=(int *)malloc(n * sizeof(int));
    for(i = 0; i < m; i++) {
        max[i] = (int *)malloc(n * sizeof(int));
        need[i]=(int *)malloc(n * sizeof(int));
        alloc[i]=(int *)malloc(n * sizeof(int));
    }
    for(i=0;i<m;i++) {
        flag[i]=0;
        sum[i]=0;
    }
    printf("\nEnter the instances of %d resources :",n);
    for(i=0;i<n;i++) {
        scanf("%d",&res[i]);
    }
    printf("\nEnter MAX matrix :\n");
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
            scanf("%d",&max[i][j]);
    printf("\nEnter Allocation matrix :\n");
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
            scanf("%d",&alloc[i][j]);
    printf("\nNeed matrix : ");
    for(i=0;i<m;i++) {
        for(j=0;j<n;j++) {
            need[i][j]=max[i][j]-alloc[i][j];
        }
    }
    for(i=0;i<m;i++) {
        for(j=0;j<n;j++) {
            printf("%d ",need[i][j]);
        }
        printf("\n");
    }
    for(i=0;i<n;i++) {
        for(j=0;j<m;j++) {
            sum[i]+=alloc[j][i];
        }
    }
    printf("\nSUM matrix :");
    for(i=0;i<n;i++) {
        printf("%d ",sum[i]);
    }
    for(i=0;i<n;i++)
        avail[i]=res[i]-sum[i];
    printf("compare\n");
    for(i=0;;i++) {
        i=i%m;
        count=0;
        if(c==m)
            break;
        if(flag[i]==1) {
            c++;
            continue;
        }
        for(j=0;j<n;j++) {
            if(avail[j]>=need[i][j]) {
                count++;
            }
        }
        if(count==3) {
            printf("\nProcess-p%d cleared\t",i);
            for(j=0;j<n;j++) {
                avail[j]+=alloc[i][j];
                flag[i]=1;
                printf("%d  ",avail[j]);
            }
        }
    }
    printf("\nDONE!");
}
[B]

OPRA ALGORITHM[/B]

Code:
#include<stdio.h>
int arr[20]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
int fr[3]={7,0,1};
int count(int p) {
    int i,j,k,l,large,index=0;
    int c[3]={0,0,0};
    for(j=0;j<3;j++) {
        for(i=p;i<20;i++) {
            if(fr[j]==arr[i]) 
                break;
            else
                c[j]++;
        }
    }
    large=c[0];
    for(i=1;i<3;i++) {
        if(large<c[i]) {
            large=c[i];
            index=i;
        }
    }
    return index;
}
void display() {
    int i;
    for(i=0;i<3;i++)
        printf("%d \t",fr[i]);
    printf("\n");
}

int main() {
    int i,j,k,l;
    int index;
    for(i=3;i<20;i++) {
        if(fr[0]==arr[i] || fr[1]==arr[i] || fr[2]==arr[i])
                continue;
        index=count(i);
        fr[index]=arr[i];
        display();
    }
}
[/CODE}
 

╬Switch╬

In the zone
Re: Post ur C/C++ Programs Here

Hi, Im learning a lot of C programming from this thread, thanks to all the great people contributing.
I just tried a program:
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
 int a=4,b,c;
 clrscr();
 b=a++*a++;
 printf("b=%d",b);
 a=4;
 a=a++*a++;
 printf("\na=%d",a);
 getch();
}
Why does b show 16 while a=18?
This is what I understand:
b=a++*a++ is evaluated as b=a*a and then the incrementation occurs, but I can't understand the next o/p, ie "a".
 

Kl@w-24

Slideshow Bob
Re: Post ur C/C++ Programs Here

#include<stdio.h>
#include<conio.h>
void main()
{
int a=4,b,c;
clrscr();
b=a++*a++;
printf("b=%d",b);
a=4;
a=a++*a++;
printf("\na=%d",a);
getch();
}

I can't understand the next o/p, ie "a".

The next operation goes as follows:

You wrote a = a++ * a++;

a=4*4

a=16

Now, twice a++:

a=16+1=17
a=17+1=18

a=18

The post-increment occurs after the multiplication has been performed, and since the val of a is now 16, after incrementing it twice, it becomes 18.
 

╬Switch╬

In the zone
Re: Post ur C/C++ Programs Here

^^Thanks :)
One more thing, I just realised that TC is ancinet, so Im trying to find a newer editor+compiler.
I think Ill go with Rleo for the editor and whats the difference between G++ and and MinGW.
Is there any other editor better than Relo?
 
Last edited:

Liverpool_fan

Sami Hyypiä, LFC legend
Re: Post ur C/C++ Programs Here

Hi, Im learning a lot of C programming from this thread, thanks to all the great people contributing.
I just tried a program:
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
 int a=4,b,c;
 clrscr();
 b=a++*a++;
 printf("b=%d",b);
 a=4;
 a=a++*a++;
 printf("\na=%d",a);
 getch();
}
Why does b show 16 while a=18?
This is what I understand:
b=a++*a++ is evaluated as b=a*a and then the incrementation occurs, but I can't understand the next o/p, ie "a".

Keep in mind to avoid using code such as b = a++ + a++ or a = a++ + a++. No I am not joking. C is not owned by a community or a vendor that dictates the way the language behaves. So as a result particulary in case of post-fix and pre-fix operations there is the side-effect whose behaviour depends on the implementation details of the compiler. For instance if I compile these statements with gcc -Wall (which means using GCC compiler with ALL warnings enabled) then, I get the Warning:
gcc -Wall said:
abc.c:7: warning: operation on ‘a’ may be undefined
In language such as Java and C#, since the language is owned by a company/community, so in those languages one doesn't have to worry about such details since each compiler. But in C it depends from compiler to compiler.

Though only for learning, try all permutations and combinations of the post fix and pre fix operations and learn them (they are pretty popular VIVA questions) but just keep on mind the implementation varies from compiler to compiler.

Thus my advice would be that beyond the scope of learning, avoid using statements such as these in C.

EDIT: Few links:
*bytes.com/groups/c/222558-operation-x-may-undefined
*c-faq.com/expr/evalorder2.html

P.S. If this confuses you, you can easily ignore the above post. :D

^^Thanks :)
One more thing, I just realised that TC is ancinet, so Im trying to find a newer editor+compiler.
I think Ill go with Rleo for the editor and whats the difference between G++ and and MinGW.
Is there any other editor better than Relo?

Just look at my sig. MinGW is the compiler (Minimalist GNU for Windows) and Geany is the IDE. Set 'em up. :)
 
Last edited:

Liverpool_fan

Sami Hyypiä, LFC legend
Re: Post ur C/C++ Programs Here

^ ^ ^
For normal exits, use exit(0), for abnormal termination use exit(1), exit(2)....depending on the error code. (0-127 IIRC)

Also take note...for more clarity of code:
#include <stdlib.h>

Code:
exit(EXIT_SUCCESS); /*equivalent to exit(0) */
exit(EXIT_FAILURE); /*equivalent to exit(1) or it may range from 1-127*/

(also you could you these variables with the return statements)
 

╬Switch╬

In the zone
Re: Post ur C/C++ Programs Here

^ ^ ^
For normal exits, use exit(0), for abnormal termination use exit(1), exit(2)....depending on the error code. (0-127 IIRC)

Also take note...for more clarity of code:
#include <stdlib.h>

Code:
exit(EXIT_SUCCESS); /*equivalent to exit(0) */
exit(EXIT_FAILURE); /*equivalent to exit(1) or it may range from 1-127*/
(also you could you these variables with the return statements)
I did some search and I think that normal exit is when you want to stop the program after a successful operation and abnormal exit is when you stop the program after an error. Do I sound right?
 

rajkishor09

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

hello, i have found a good c program but cannot post that here due to copyright issues you can follow that here.
*hubpages.com/_iinfo/hub/File-Copy-Program-in-C-Language

*hubpages.com/_iinfo/hub/Turn-Your-PC-Keyboard-to-Musical-Keyboard
 
Status
Not open for further replies.
Top Bottom