Post your C/C++ Programs Here

Status
Not open for further replies.

teejay_geekEd

GeekEd!...
Re: Post ur C/C++ Programs Here

can you help me wid a simple program-- to convert all vowels in a file from upper to lower case and vice-versa using simple file handling concept.?
thanks
 

crazyvalentine

Right off the assembly line
a simple program to convert decimal into binary

#include<iostream.h>
#include<conio.h>
void main()
{
int n,p=1,s=0;
clrscr();
cout<<"enter any number";
cin>>n;
while(n>0)
{
k=n%2;
s=s+k*p;
n=n/2;
p=p*10;
}
cout<<s;
getch();
}
 

QwertyManiac

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

can you help me wid a simple program-- to convert all vowels in a file from upper to lower case and vice-versa using simple file handling concept.?
thanks
Code:
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

char lower[] = {'a','e','i','o','u'};
char upper[] = {'A','E','I','O','U'};

char vowel(char a) {
    for(int i=0;i<5;i++) {
        if (a==lower[i]) { return a-32; }
        if (a==upper[i]) { return a+32; }
    }
    return a;
}

int main(int argc, char **argv) {
    ifstream input(argv[1], ifstream::in);
    string ip, op;
    while (input.good()) { getline(input, ip); op.append(ip+"\n"); ip.erase(); }
    transform(op.begin(), op.end(), op.begin(), vowel);
    cout << op;
    return 0;
}

Swaps lower vowel to upper and upper to lower and prints to stdout.
 

QwertyManiac

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

I don't mind learning stuff myself while helping others (or not). Call me selfish, I don't care. :))
 

ambika

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

Someone assign me the algorithm or code for the same .

1. Wite aii interactive C program that outputs a telephone bill, if the number of telephone calls
are given by the user. A telephone call is described by the starting and stopping timqs, where
. the timings are given in the form of hh:mm,/dd/mm/VyW 20 .
hh + the hour 'dd -+ the day
mm -) the minutes mm -+ the month
yyyy -) the year ,
,:The'fo[owtng table describes the cost of a phone call per minute.
Time Cost per minute (in rupees)
00:00 to 7:59
08:00 to 18:59
19;00 to 23:59
0.50
1.00
0.40






1. Write an interactive C program to generate a pay-slip for 7 employees in a small organisation
whose Basic, TA, DA, Allowances, Perks, Deductions (like GPF, loans, LIC) are given by the
user. The pay-slip should contain the name, scale of pay, month and year, designation,
department, gross-pay, net-pay and attendance.
 
Last edited:

Yamaraj

The Lord of Death
Re: Post ur C/C++ Programs Here

Code:
if (a==lower[i]) { return a-32; }
if (a==upper[i]) { return a+32; }
Instead of using magic numbers in a program, you should make use of library functions to convert the character to upper or lower case.
 

ambika

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

MERRY CHRISTMAS TO ALL.....


Code:
/*program written by AMBIKA for giving you best wishes for christmas and new year*/

#include<stdio.h>

void main()

{

char name;

int choice;

do
{

printf("\nPlease enter your first name here:");

scanf("%s",&name);

printf("\n Hello!! %s!!,How are you ?",&name);

printf("\n Please enter your choice and get my best wishes for you.Thanks!!\n");

printf("\n ***************** 1.Wishes for a good day************************\n");

printf("\n ***************** 2.Wishes for a christmas************************\n");

printf("\n ***************** 3.Wishes for the new year**********************\n");

printf("\n ***************** 4.Exit ****************************************\n");

scanf("%d",&choice);

switch(choice)

{

case 1:printf("\n Have a good day %s!!",&name);
break;

case 2:printf("\n Merry christmas %s!!",&name);
break;

case 3:printf("\n HAPPY NEW YEAR 2009 ,Wishing u a prosperus new year %s.",&name);
break;

case 4:printf("\n THANK YOU ,BYE!!");
break;

default:printf("\nInvalid Entry.Please enter right choice.\n");
break;
}

}while(choice!=4); return 0;

}

/*program ends here.*/
 

QwertyManiac

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

Instead of using magic numbers in a program, you should make use of library functions to convert the character to upper or lower case.
Ah, yes! :)

Diff patch fix (newvowelswap.cpp):
Code:
--- oldvowelswap.cpp    2008-12-24 18:45:13.000000000 +0530
+++ newvowelswap.cpp    2008-12-24 18:44:55.000000000 +0530
@@ -1,6 +1,7 @@
 #include <iostream>
 #include <fstream>
 #include <algorithm>
+#include <string>
 
 using namespace std;
 
@@ -9,8 +10,8 @@
 
 char vowel(char a) {
     for(int i=0;i<5;i++) {
-        if (a==lower[i]) { return a-32; }
-        if (a==upper[i]) { return a+32; }
+        if (a==lower[i]) { return toupper(a); }
+        if (a==upper[i]) { return tolower(a); }
     }
     return a;
 }
 

QwertyManiac

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

That's cause the standard library functions all belong to the std namespace. So instead of using those functions cin, fstream, etc ... as std::cin, std::fstream, etc ..., we just add the using namespace std; line and its automatically done.
 

nvidia

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

Guys whats the Python equivalent of the C infinite for loop?
Code:
for(i = 0;  ; i++)

I wanted a range of about 10^25 and i cant use it in python
Code:
for x in range(1,pow(10,25)):
    somecode
When i run this code it says
Code:
Traceback (most recent call last):
  File "C:/Python26/Euler_66", line 10, in <module>
    for y in range(1,k):
OverflowError: range() result has too many items
 

Sykora

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

@nvidia:

The simplest infinite loop is

Code:
while True :
   do_something()

but if you really wanted a for loop instead of a while loop,

Code:
from itertools import count
for i in count(0) :
    do_something()
 

nvidia

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

I tried using while(True) in my program but it gets stuck somewhere infinitely.. Don't the break statements work for a while loop in Python?
 

QwertyManiac

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

@nvidia - Logic issue, not Python. Debug it :)

break always breaks the loop its nested in.
 

nvidia

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

My Bad..:oops:
My break statement was indented wrongly. :oops:
Thanks skykora and QM:)
Btw, any of you doing Project Euler?
 

QwertyManiac

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

A very simple Decimal -> Binary converter. (Correct upto sizeof(int)*8-1 bits)

Code:
[COLOR=#bc7a00]#include <stdio.h>
#include <stdlib.h>
[/COLOR]
[COLOR=#b00040]void[/COLOR] [COLOR=#0000ff]binary[/COLOR]([COLOR=#b00040]int[/COLOR] num) {
    [COLOR=#b00040]int[/COLOR] i[COLOR=#666666]=[/COLOR][COLOR=#666666]0[/COLOR];
    printf([COLOR=#ba2121]"Binary: "[/COLOR]);
    [COLOR=#008000][B]for[/B][/COLOR](i[COLOR=#666666]=[/COLOR][COLOR=#008000][B]sizeof[/B][/COLOR](num)[COLOR=#666666]*[/COLOR][COLOR=#666666]8[/COLOR][COLOR=#666666]-[/COLOR][COLOR=#666666]1[/COLOR]; i[COLOR=#666666]>-[/COLOR][COLOR=#666666]1[/COLOR]; i[COLOR=#666666]--[/COLOR]) {
        printf([COLOR=#ba2121]"%d"[/COLOR], num[COLOR=#666666]&[/COLOR]([COLOR=#666666]1[/COLOR][COLOR=#666666]<<[/COLOR]i)[COLOR=#666666]?[/COLOR][COLOR=#666666]1[/COLOR][COLOR=#666666]:[/COLOR][COLOR=#666666]0[/COLOR]);
    }
    printf([COLOR=#ba2121]"[/COLOR][COLOR=#bb6622][B]\n[/B][/COLOR][COLOR=#ba2121]"[/COLOR]);
}
[COLOR=#b00040]int[/COLOR] [COLOR=#0000ff]main[/COLOR]([COLOR=#b00040]int[/COLOR] argc, [COLOR=#b00040]char[/COLOR] [COLOR=#666666]**[/COLOR]argv) {
    [COLOR=#008000][B]if[/B][/COLOR] (argc[COLOR=#666666]>[/COLOR][COLOR=#666666]1[/COLOR])
        binary(atoi(argv[[COLOR=#666666]1[/COLOR]]));
    [COLOR=#008000][B]else[/B][/COLOR] {
        [COLOR=#b00040]int[/COLOR] a;
        printf([COLOR=#ba2121]"Enter a number: "[/COLOR]);
        scanf([COLOR=#ba2121]"%d"[/COLOR], [COLOR=#666666]&[/COLOR]a);
        binary(a);
    }
    [COLOR=#008000][B]return[/B][/COLOR] [COLOR=#666666]0[/COLOR];
}
 
Status
Not open for further replies.
Top Bottom