Post your C/C++ Programs Here

Status
Not open for further replies.

nithinks

True Techie
Re: Post ur C/C++ Programs Here

ilugd said:
how bout setting some encrypted string on a harddisk sector/track combination and setting the sector to damaged in the allocation table. The program can then read and update. I guess you will have to use interrupts to do that.

Nice idea.. but is it possible to code that in C? and how to write a registry entry in C?
 

ilugd

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

not a registry entry. It will have to be done with assembly or with int function calls. I am not the expert in that though. Just my two cents.
 

nithinks

True Techie
Re: Post ur C/C++ Programs Here

Okay another challenge ..
Can you write a C program to print the number in the format given below?

*img502.imageshack.us/img502/7007/20758345gu0.jpg

and so on
 

QwertyManiac

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

nithinks said:
Okay another challenge ..
Can you write a C program to print the number in the format given below?

*img502.imageshack.us/img502/7007/20758345gu0.jpg

and so on
Don't think mine's so efficient but it kind of does the job:

Code:
#include<iostream>

using namespace std;

/*

            1
           323
          43234
         5432345

*/

int main()
    {
        int count = 1, temp=1, i, j, k;
        
        for(int times=1;times<5;times++)
            {
                for(i=5;i>=times;i--)
                    {
                        cout<<" ";
                        if(i==1)
                            {
                                cout<<1;
                            }                        
                    }
                    
                for(k=temp;k>=2;k--)
                    {
                        cout<<k;
                    }
                
                
                    
                for(j=3;j<=temp;j++)
                    {
                        cout<<j;
                    }
                
                if(temp==1)
                    {
                        temp+=2;
                    }
                else
                    {
                        temp++;
                    }
                
                count+=1;
                    
                cout<<endl;
            }
    }
 

Desi-Tek.com

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

encryption in java
Code:
package ocricket.api.encryption;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

/**

* Encodes a string using MD5 hashing

*

* @author Dheeraj

* @version 1.0

*/

public class MD5

{

/**

* Encodes a string

*

* @param str String to encode

* @return Encoded String

*/

public static String crypt(String str)

{

if (str == null || str.length() == 0) {

throw new IllegalArgumentException(“String to encript cannot be null or zero length”);

}

StringBuffer hexString= new StringBuffer();

try {

MessageDigest md = MessageDigest.getInstance(“MD5″);

md.update(str.getBytes());

byte[] hash = md.digest();

for (int i = 0; i < hash.length; i++) {

if ((0xff & hash[i]) < 0×10) {

hexString.append(“0″ + Integer.toHexString((0xFF & hash[i])));

}

else {

hexString.append(Integer.toHexString(0xFF & hash[i]));

}

}

}

catch (NoSuchAlgorithmException e) {

}

return hexString.toString();

}

}
here is the tutorial on how to use it *desi-tek.org/blog/2007/09/19/tutorial-on-how-to-use-encryption-in-java-web-application.html
 

Projjwal

free world from money
Re: Post ur C/C++ Programs Here

De Md5 program in c++.It's little bit complicated .......
-----------------------------------------------------------------
MD5.cpp:*www.koders.com/cpp/fid5F95FCF5C140CE260F9D28AD5ECB21531ACAE8C6.aspx?s=md5
md5.h:*www.koders.com/c/fidACD7EC6E0FCD2218463A664206B8BD97237711CC.aspx?s=md5
------------------------------------------------------------------
there r two files md5.cpp & md5.h(the header file).
But it's complicated for me . I used md5 algorithm in my VC# project where it build in. (I use Microsoft VC# 2005)
 

Glen Apayart

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

This is my first program in c++.

Actually its working well and i just want to know the comments or suggestion about my program. i know it still need more code to make it stable and effective.

Program: a simple shop using string, integers, float, & algebraic operators
Code:
#include <iostream>
using namespace std;

int main()
{
    char firstName[20], lastName[20];

    unsigned short shirts;
    unsigned short pants;
    unsigned short shoes;
    unsigned short caps;
    unsigned short totalItems;

    double priceShirts = 1.25;
    double pricePants = 2.75;
    double priceShoes = 3.25;
    double priceCaps = 1.65;

    float totalShirts;
    float totalPants;
    float totalShoes;
    float totalCaps;
    float all, payment, change;

    int orderDay;
    int orderMonth;
    int orderYear;

    cout << "-=- Apayart Botique -=-";
    cout << "\nEnter your personal information";
    cout << "\nFirst Name: ";
    cin >> ws;
    cin.getline(firstName, 20);
    cout << "Last Name: ";
    cin >> ws;
    cin.getline(lastName, 20);

    cout << "\nEnter date of order";
    cout << "\nOrder Day: ";
    cin >> orderDay;
    cout << "Order Month: ";
    cin >> orderMonth;
    cout << "Order Year: ";
    cin >> orderYear;

    cout << "\nEnter number of shirts: ";
    cin >> shirts;
    cout << "Enter number of pants: ";
    cin >> pants;
    cout << "Enter number of shoes: ";
    cin >> shoes;
    cout << "Enter number of caps: ";
    cin >> caps;

    totalShirts = shirts * priceShirts;
    totalPants = pants * pricePants;
    totalShoes = shoes * priceShoes;
    totalCaps = caps * priceCaps;
    totalItems = shirts + pants + shoes + caps;
    all = totalShirts + totalPants + totalShoes + totalCaps; 

    cout << "\ntotal price: $"; << all;
    cout <<"\nYour payments: $";
    cin >> payment;

    change = payment - all;

    cout << "\n=======================";
    cout << "\n-=- Apayart Botique -=-";
    cout << "\n=======================";
    cout << "\nCustomer Order";
    cout << "\n\tCustomer Name: " << firstName << " " << lastName;
    cout << "\n\tOrder Date: ";
    cout << orderMonth << "/" << orderDay << "/" << orderYear;
    cout << "\n------------------------------";
    cout << "\nItems type    Qty     Price";
    cout << "\n------------------------------";
    cout << "\nShirts        " << shirts << "     $" << totalShirts;
    cout << "\nPants        " << pants << "     $" << totalPants;
    cout << "\nShoes        " << shoes << "     $" << totalShoes;
    cout << "\nCaps        " << caps << "     $ "<< totalCaps;
    cout << "\n------------------------------;
    cout << "\nTotal Items: "; << totalItems;
    cout << "\n\nTotal Price: $" << all;
    cout << "\nPayment: $" << payment;
    cout << "\nChange: $" << change << "\n\n\n\n\n\n";

    return 0;

}
 
Last edited:

Glen Apayart

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

@aditya.shevade

Thanks! i saw your program which is &quot;Bulls and Cows&quot; and i observe your using the techniques of passing arguments. Im into that from now. Maybe later i'll put my code about passing arguments.

I feel good when someone shares idea to each other. ^^

Edit:.

Here is the code with passing arguments of my simple shop.

Code:
#include <iostream>
#include <string>
using namespace std;

string getUser()
{
    string firstName, lastName, FN;

    cout << "-=- Apayart Botiques-=-";
    cout << "\nEnter your personal information";
    cout << "\nFirst Name: ";
    cin >> firstName;
    cout << "Last Name: ";
    cin >> lastName;

    FN = firstName + " " + lastName;

    return FN;
}

int main()
{
    string user;
    user = getUser();

    unsigned short shirts;
    unsigned short pants;
    unsigned short shoes;
    unsigned short caps;
    unsigned short totalItems;

    double getShirts(unsigned short shirts);
    double getPants(unsigned short pants);
    double getShoes(unsigned short shoes);
    double getCaps(unsigned short caps);

    float totalShirts;
    float totalPants;
    float totalShoes;
    float totalCaps;
    float all, payment, change;

    int orderDay, orderMonth, orderYear;

    cout << "Enter the date of order";
    cout << "\nOrder Day: ";
    cin >> orderDay;
    cout << "Order Month: ";
    cin >> orderMonth;
    cout << "Order Year: ";
    cin >> orderYear;

    cout << "\nEnter number of shirts: ";
    cin >> shirts;
    cout << "Enter number of pants: ";
    cin >> pants;
    cout << "Enter number of shoes: ";
    cin >> shoes;
    cout << "Enter number of caps: ";
    cin >> caps;

    totalShirts = getShirts(shirts);
    totalPants = getPants(pants);
    totalShoes = getShoes(shoes);
    totalCaps = getCaps(caps);

    totalItems = shirts + pants + shoes + caps;
    all = totalShirts + totalPants + totalShoes + totalCaps; 

    cout << "\nTotal price: $" << all;
    cout <<"\nYour payments: $";
    cin >> payment;

    change = payment - all;

    cout << "\n========================&quot;;
    cout << "\n-=- Apayart Botiques -=-&quot;;
    cout << "\n========================&quot;;
    cout << "\nCustomer Order&quot;;
    cout << "\n\tCustomer Name: &quot; << user;
    cout << "\n\tOrder Date: " << orderMonth << "/" << orderDay << "/" << orderYear;
    cout << "\n------------------------------";
    cout << "\nItems type    Qty     Price";
    cout << "\n------------------------------";
    cout << "\nShirts        " << shirts << "     $" << totalShirts;
    cout << "\nPants        " << pants << "     $" << totalPants;
    cout << "\nShoes        " << shoes << "     $" << totalShoes;
    cout << "\nCaps        " << caps << "     $" << totalCaps;
    cout << "\n------------------------------";
    cout << "\nTotal Items: " << totalItems;
    cout << "\n\nTotal Price: $" << all;
    cout << "\nPayment: $" << payment;
    cout << "\nChange: $" << change << "\n\n\n\n\n\n";

    return 0;
}

double getShirts(unsigned short shirts)
{
    double priceShirts = 1.25;
    float totalShirts;

    totalShirts = shirts * priceShirts;

    return totalShirts;
}

double getPants(unsigned short pants)
{
    double pricePants = 2.75;
    float totalPants;

    totalPants = pants * pricePants;

    return totalPants;
}

double getShoes(unsigned short shoes)
{
    double priceShoes = 3.25;
    float totalShoes;

    totalShoes = shoes * priceShoes;

    return totalShoes;
}

double getCaps(unsigned short caps)
{
    double priceCaps = 1.65;
    float totalCaps;

    totalCaps = caps * priceCaps;

    return totalCaps;
}
 
Last edited:

Sykora

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

^^^ I think it's a bit neater to put your function prototypes above main, and not in it. Or you can put main below everything else, which is what I do. Not really an issue, but just my 2 cents.

It's a good thing you've started out programming in C++ with a good coding style, as aditya.shevade mentioned. Stick to it.
 

QwertyManiac

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

Nav11aug said:
yup... funcs prototyped inside main() wont be accesible by ne other funcs
What about a prototyped function accessing another prototyped function (Both belonging to the same function)?

I don't understand this:
Code:
#include<iostream>

//    Working Example

using namespace std;

int main()
    {
        void show1(); //    Prototyped Declarations
        void show2(); //        "           "
        
        show1();
        show2();
        
        cout<<endl;
        return 0;
    }

void show2()
    {
        cout<<" World";
    }

void show1()
    {
        cout<<"Hello";
        show2();  //Show1() calls similarly prototyped Show2()
    }
This one (above) executes perfectly but this one (below) doesnt:
Code:
#include<iostream>

//    Non-Working Example

using namespace std;

int main()
    {
        void show1(); //    Prototyped Declarations
        void show2(); //        "           "
        
        show1();
        show2();
        
        cout<<endl;
        return 0;
    }


void show1()
    {
        cout<<"Hello";
        show2();  //Show1() calls similarly prototyped Show2()
    }

void show2()
    {
        cout<<" World";
    }

/*      harsh@qwerty-workstation:~$ g++ test.cpp 
test.cpp: In function ‘void show1()’:
test.cpp:23: error: ‘show2’ was not declared in this scope      */
So commonly prototyped functions can access each other only if they are ordered in a particular (sort of global) heirarchy? Isn't this too restrictive?

And what would this type of recursion be called?
Code:
#include<iostream>

[B]//    NOT FOR EXECUTION.
//    Improper code, ends in Segmentation fault
//    Posted for understanding, not running purposes
[/B]
using namespace std;

void show1();
void show2();

void show2()
    {
        cout<<" World";
        show1();
    }

void show1()
    {
        cout<<"Hello";
        show2();
    }

int main()
    {
        show1();
        show2();
        
        cout<<endl;
        return 0;
    }

Edit: Err leave the last one, its stupid. Can't be called a recursion either, as I figured.
 
Last edited:

Nav11aug

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

@qwerty :

Which compiler do u use .As far as i remember readin in K&R , he says whther the order matters depends only on the compiler .

But still , i dont think i get ne reason. :/ ne1 else ,plz?

Off-topic: i just realized tht urs was one of teh easiest names type out "qwerty" :)
 

Sykora

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

@Qwerty :

Your first two examples make sense -- The first one works because the definition of show2() is above its function call in show1(). It works in main because it has been prototyped in main(). In the second example, you've interchanged the positions of show1() and show2(), and it doesn't work, becuase show1() doesn't know that show2() exists.

Prototypes are like variable declarations, they lose scope unless declared globally, which is what I suggested.

Your last example isn't stupid at all. It's a technique called mutual recursion. Obviously its practical uses aren't as simple as what you've written, but if the termination conditions are written properly, it can be very powerful. In fact, one of the best Sudoku solvers I've seen uses a combination of depth-first-search and mutual recursion. I'll see if I can find that code.

I'm pretty sure that the order matters irrespective of the compiler, as long as it conforms to a certain degree of standards.
 

Nav11aug

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

my compiler (gcc as u cn see) :

Code:
#include<stdio.h>

int main()
{
    void func1(void );
    void func2(void );
    
    func2();
    func1();
    
    getchar();
    
    return 0;
}

void func2(void )
{
    printf("Wrkin?");
    func1();
}

void func1(void )
{
    printf("\nIn func1 now");
}

g++.exe C:\gcc\Source\protoinmain1.o -o C:\gcc\Source\protoinmain1.exe -Wall -fpermissive
C:\gcc\Source\protoinmain1.o(.text+0x43): protoinmain1: undefined reference to `func1'
Failure
 

aditya.shevade

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

^^ Nothing (1st and 2nd) working here with gcc 4.1, OpenSuSE 10.2.

@ Glen, I am also new to C++. I had been using C for long time. It's been only about a month with C++ (in terms of learning, 30 days).

@ Glen, Listen to Sykora. It's better to declare the functions before main and then you can either define them after main of define them right before main along with the declaration.
 
Last edited:

Desi-Tek.com

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

Projjwal said:
De Md5 program in c++.It's little bit complicated .......
-----------------------------------------------------------------
MD5.cpp:*www.koders.com/cpp/fid5F95FCF5C140CE260F9D28AD5ECB21531ACAE8C6.aspx?s=md5
md5.h:*www.koders.com/c/fidACD7EC6E0FCD2218463A664206B8BD97237711CC.aspx?s=md5
------------------------------------------------------------------
there r two files md5.cpp & md5.h(the header file).
But it's complicated for me . I used md5 algorithm in my VC# project where it build in. (I use Microsoft VC# 2005)
try to convert my program into c# using c# converter?

i'll soon post the tutorial on JPA and hibernate which has changed the way we use to communicate with database in java and .net.
here is one of the video tutorial on it
*www.thescreencast.com/2007/07/tour-around-europe-eclipse-for-java-ee.html

and some resources
hibernate in dot net
NHibernate

*www.hibernate.org/365.html?cmd=prntdoc

hibernate in java
*www.hibernate.org/
JPA
*java.sun.com/developer/technicalArticles/J2EE/jpa/
 
Last edited:

Glen Apayart

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

@Sykora
Thanks for the advice. So, the only function that are considered to be define is the inline function.. Any other circumstances?

@aditya.shevade

Yes, Sykora is right about it. i'll stick to that.
 

Sykora

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

Glen Apayart said:
So, the only function that are considered to be define is the inline function.. Any other circumstances?

Can you bring that one past me again? I didn't understand that at all.
 
Status
Not open for further replies.
Top Bottom