Week #14 Challenge: C++

  • (5 Pages)
  • +
  • « First
  • 3
  • 4
  • 5

67 Replies - 29156 Views - Last Post: 13 October 2010 - 08:11 AM

#61 Kohana  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 56
  • Joined: 09-January 10

Re: Week #14 Challenge: C++

Posted 19 April 2010 - 07:53 AM

View Postaniri, on 19 April 2010 - 05:37 AM, said:

So, who won? :P



I know right!
Was This Post Helpful? 0
  • +
  • -

#62 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: Week #14 Challenge: C++

Posted 20 April 2010 - 03:34 PM

Made a few changes to bodom658's Complex class (post #8) and used it to compute the Mandelbrot set. You will need to convert the text output to a plot (I used R).

complex.h:
#ifndef COMPLEX_BODOM658
#define COMPLEX_BODOM658

#include<iostream>

using std::ostream;

class Complex {
        double real;
        double imag;

public:
        Complex(double r, double i);
        double getReal();
        double getImag();

        double mod();

        friend Complex operator + (Complex& a, Complex& B)/>;
        friend Complex operator - (Complex& a, Complex& B)/>;
        friend Complex operator * (Complex& a, Complex& B)/>;
        friend Complex operator / (Complex& a, Complex& B)/>;
        friend ostream& operator << (ostream& out, Complex a);
};

#endif



complex.cpp:
#include "complex.h"

#include <cmath>

// Methods
Complex::Complex(double r, double i) : real(r), imag(i) {}
double Complex::getReal() { return real; }
double Complex::getImag() { return imag; }
double Complex::mod() { return sqrt(real*real + imag*imag); }

// Friends
Complex operator + (Complex& a, Complex& B)/> {
        return Complex(a.getReal() + b.getReal(),
                        a.getImag() + b.getImag());
}

Complex operator - (Complex& a, Complex& B)/> {
        return Complex(a.getReal() - b.getReal(),
                        a.getImag() - b.getImag());
}

Complex operator * (Complex& a, Complex& B)/> {
        return Complex(a.getReal()*b.getReal()-a.getImag()*b.getImag(),
                        a.getReal()*b.getImag()+a.getImag()*b.getReal());
}

Complex operator / (Complex& a, Complex& B)/> {
        double coeff = (1.0/(b.getReal()*b.getReal() + b.getImag()*b.getImag()));
        return Complex(coeff*(a.getReal()*b.getReal() + a.getImag()*b.getImag()),
                coeff*(a.getImag()*b.getReal() - a.getReal()*b.getImag()));
}

ostream& operator << (ostream& out, Complex a) {
        if(a.getImag() > 0)
                out << a.getReal() << "+" << a.getImag() << "j";
        else if (a.getImag() == 0)
                out << a.getReal();
        else
                out << a.getReal() << a.getImag() << "j";
        return out;
}



main.cpp:
#include <iostream>
#include <cstdlib>

#include "complex.h"

using namespace std;

int main(int argc, char** argv) {

	if (argc != 6) {
		cerr << "Usage: " << argv[0] << " min_x max_x min_y max_y step" << endl;
		return 1;
	}

	double minx = atof(argv[1]);
	double maxx = atof(argv[2]);
	double miny = atof(argv[3]);
	double maxy = atof(argv[4]);
	double step = atof(argv[5]);

	int iterations = 64;
	double bound = 2;

	// header
	cout << "x,y,iterations\n";

	for (double x = minx; x <= maxx; x += step) {
		for (double y = miny; y <= maxy; y += step) {
			Complex c(x,y);
			Complex z(0,0);
			double crtmod;
			int i;
			for (i = 0; i < iterations; i++) {
				z = z*z;
				z = z + c;
				crtmod = z.mod();
				if (crtmod > bound) {
					break;
				}
			}
			cout << x << "," << y << "," << i << endl;
		}
	}

	return 0;
}


Was This Post Helpful? 1
  • +
  • -

#63 cdog2cool4u  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 22-September 09

Re: Week #14 Challenge: C++

Posted 21 April 2010 - 10:13 AM

i was away from my computer last week but would you count a magic 8 ball as a simple question->answer chat application
Was This Post Helpful? 0
  • +
  • -

#64 ladyinblack  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 9
  • View blog
  • Posts: 419
  • Joined: 08-April 09

Re: Week #14 Challenge: C++

Posted 03 May 2010 - 07:01 AM

I did a calculator in Qt4, part of my assignment. Some may like, some may not, me, not particularly.

Calculate.h:

#ifndef CALCULATE_H
#define CALCULATE_H

#include <QApplication>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>

class Calculate : public QApplication
{
    Q_OBJECT
    public:
        Calculate(int argc, char * argv[]);
        ~Calculate();

    public slots:
        void calcAdd();         // to calculate addition
        void calcSubtract();    // to calculate subtraction
        void calcMultiply();    // to calculate multiplication
        void calcDivide();      // to calculate division
        void quit();            // to quit the application

    private:
        QWidget * widget;
        QHBoxLayout * hLayout1;
        QVBoxLayout * vLayout1;
        QVBoxLayout * vLayout2;
        QVBoxLayout * vLayout3;
        QLineEdit * edit1;
        QLineEdit * edit2;
        QLabel * label;
        QPushButton * add;
        QPushButton * subtract;
        QPushButton * multiply;
        QPushButton * divide;
        QPushButton * exit;
};

#endif // CALCULATE_H



Calculate.cpp:

#include "Calculate.h"

//Calculate::Calculate(QString msg, QWidget * parent)
Calculate::Calculate(int argc, char * argv[])
        : QApplication(argc, argv)
{
    // INITIALIZE WIDGETS
    widget = new QWidget();
    hLayout1 = new QHBoxLayout();
    vLayout1 = new QVBoxLayout();
    vLayout2 = new QVBoxLayout();
    vLayout3 = new QVBoxLayout();
    edit1 = new QLineEdit();
    edit2 = new QLineEdit();
    label = new QLabel();
    add = new QPushButton("Add");
    subtract = new QPushButton("Subtract");
    multiply = new QPushButton("Multiply");
    divide = new QPushButton("Divide");
    exit = new QPushButton("Exit");

    // LAYOUT OF WIDGETS
    // vertical layout 1
    vLayout1->addWidget(edit1);
    vLayout1->addWidget(edit2);
    vLayout1->addWidget(label);
    // vertical layout 2
    vLayout2->addWidget(add);
    vLayout2->addWidget(subtract);
    vLayout2->addStretch();
    // vertical layout 3
    vLayout3->addWidget(multiply);
    vLayout3->addWidget(divide);
//    vLayout3->addStretch();
    vLayout3->addWidget(exit);
    // horizontal layout
    hLayout1->addLayout(vLayout1);
    hLayout1->addLayout(vLayout2);
    hLayout1->addLayout(vLayout3);
    widget->setLayout(hLayout1);
    widget->show();

    // CONNECTING SIGNALS TO SLOTS
    connect(add, SIGNAL(clicked()), this, SLOT(calcAdd()));
    connect(subtract, SIGNAL(clicked()), this, SLOT(calcSubtract()));
    connect(multiply, SIGNAL(clicked()), this, SLOT(calcMultiply()));
    connect(divide, SIGNAL(clicked()), this, SLOT(calcDivide()));
    connect(exit, SIGNAL(clicked()), this, SLOT(quit()));
//    connect(quit, SIGNAL(triggered()), this, SLOT(close()));
}

Calculate::~Calculate()
{
    delete hLayout1;
    delete vLayout1;
    delete vLayout2;
    delete vLayout3;
    delete widget;
}

void Calculate::calcAdd()
{
    int num1 = edit1->text().toInt();
    int num2 = edit2->text().toInt();
    int sum = num1 + num2;
    label->setText(QString::number(sum, 10));
}

void Calculate::calcSubtract()
{
    int num1 = edit1->text().toInt();
    int num2 = edit2->text().toInt();
    int difference = num1 - num2;
    label->setText(QString::number(difference, 10));
}

void Calculate::calcMultiply()
{
    int num1 = edit1->text().toInt();
    int num2 = edit2->text().toInt();
    int product = num1 * num2;
    label->setText(QString::number(product, 10));
}

void Calculate::calcDivide()
{
    double num1 = edit1->text().toDouble();
    double num2 = edit2->text().toDouble();
    double quotient = num1 / num2;
    label->setText(QString::number(quotient, 'g', 10));
}

void Calculate::quit()
{
    QApplication::quit();
}



calculateMain.cpp:

#include <QApplication>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QString>
#include <QPushButton>
#include "Calculate.h"

int main(int argc, char * argv[])
{
    Calculate app(argc, argv);
    return app.exec();
}


This coding requires Qt Creator installed - http://qt.nokia.com/downloads.

To admin: I'm not able to insert my codes into code links, for some reason, majority of the links are not working whenever I load it up from my work computer.


Mod Edit: added code tags.

This post has been edited by NickDMax: 08 May 2010 - 06:55 AM

Was This Post Helpful? 0
  • +
  • -

#65 Whitellama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-May 10

Re: Week #14 Challenge: C++

Posted 25 June 2010 - 06:18 PM

I did this when learning C++. It sorts an array and I think it goes from greatest to least. The array of numbers is the same every time so it's not very interesting. The purpose of this program was mostly to practice for loops and it looks pretty good to me.
#include "stdafx.h"
#include <algorithm>
#include <iostream>

int main()
{
	using namespace std;

	const int nSize = 10;
	int anArray[nSize] = { 30, 50, 20, 10, 40, 70, 100, 60, 90, 80 };

	for (int nStartIndex = 0; nStartIndex < nSize; nStartIndex++)
	{
		int nLargestIndex = nStartIndex;

		for (int nCurrentIndex = nStartIndex + 1; nCurrentIndex < nSize; nCurrentIndex++)
		{
			if (anArray[nCurrentIndex] > anArray[nLargestIndex])
				nLargestIndex = nCurrentIndex;
		}

	swap(anArray[nStartIndex], anArray[nLargestIndex]);

	}

	for (int nPrint = 0; nPrint < nSize; nPrint++)
		cout << anArray[nPrint] << endl;

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#66 Blackllama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-June 10

Re: Week #14 Challenge: C++

Posted 27 June 2010 - 12:19 AM

This is fairly simple. It takes two integers inputted by the user, multiplies them, and prints the result.

 #include <iostream>

int main()
{
    using namespace std;
    int y;
    int x;
    cout >> "Enter a number to have it multiplied by another number:";
    cin << x << End1;
    cout >> "Enter second number:"
    cin << y;
    cout >> x * y "This is the products of your numbers." << End1;
    return 0;
} 

Was This Post Helpful? 0
  • +
  • -

#67 JesseLord  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 42
  • Joined: 24-May 10

Re: Week #14 Challenge: C++

Posted 30 July 2010 - 06:48 PM

This was my final project I wrote for my C++ class. I did it in about two days. There are seven class types including examples of inheritance and composition with some polymorphism. This is a very simple paranoia character creation program. I attached the files in the zip file. Enjoy!

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#68 ShadowedSoldier313  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-October 10

Re: Week #14 Challenge: C++

Posted 13 October 2010 - 08:11 AM

i know i was late but i would like to post my code for this.


#include<iostream>
#include<math.h>
#include<string>

using namespace std;
int option;
int shortside,midside,hypotenuse;
double shortsquare=shortside*shortside;
double midsquare=midside*midside;
double hyposquare=hypotenuse*hypotenuse;
double hyposquareb,midsquareb,shortsquareb;

double PythagoreanTriples()
{
double a,b,c;
cout<<" I will need you to enter a series of numbers, to see if they are a Pythagorean Triple"<<endl;
cin>>a;
cout<<"Enter the second number"<<endl;
cin>>b;
cout<<"Enter the last number"<<endl;
cin>>c;
if ((pow(a,2)+pow(b,2)==pow(c,2)))
cout<<"This is a Pythagorean triple congrats."<<endl;
else
cout<<"This is not a Pythagorean triple try again"<<endl;
system("pause>nul");

return(0);
}
double PythagoreanTheorem()
{
cout<<"Welcome to the pythagorean theorem program"<<endl;
cout<<"What sides do u know?"<<endl;
cout<<"Enter the short side"<<endl;
cin>>shortside;
cout<<"Enter the middle side"<<endl;
cin>>midside;
cout<<"Enter the longest side"<<endl;
cin>>hypotenuse;
do
{
if((shortside==0)&&(midside==0)&&(hypotenuse==0))
{
cout<<"You don't know any of the sides. Please check your problem and try again"<<endl;
break;
}
else if((shortside!=0)&&(midside!=0)&&(hypotenuse!=0))
{
cout<<"You know all the sides you either need angles, or another theorem"<<endl;
break;
}
else if(midside>hypotenuse)
cout<<"Your middle side and hypotenuse are switched reenter"<<endl;
double shortsquare=shortside*shortside;
double midsquare=midside*midside;
double hyposquare=hypotenuse*hypotenuse;
if((shortside!=0)&&(midside!=0))
{
hyposquare=shortsquare+midsquare;
hyposquareb=sqrt(hyposquare);
cout<<"The third side is your hypotenuse of "<<hyposquareb<<endl;
}
else if((midside!=0)&&(hypotenuse!=0))
{
shortsquare=hyposquare-midsquare;
shortsquareb=sqrt(shortsquare);
cout<<"The short side is "<<shortsquareb<<endl;
}
else if((shortside!=0)&&(hypotenuse!=0))
{
midsquare=hyposquare-shortsquare;
midsquareb=sqrt(midsquare);
cout<<"The middle side is "<<midsquareb<<endl;
}
}
while(((shortside==0)&&(midside==0)&&(hypotenuse==0))||((shortside!=0)&&(midside!=0)&&(hypotenuse!=0)));
return(0);
}

double LawOfSines()
{
cout<<"welcome to the Law of Sines program"<<endl;
return(0);
}
double LawOfCosines()
{
cout<<"welcome to the Law of Cosines program"<<endl;
return(0);
}
double QuadraticFormula()
{
cout<<"welcome to the Quadratic Formula program"<<endl;
return(0);

}

int main()
{
while(option!=6)
{
cout<<"Welcome to the Math Tutor Program"<<endl;
cout<<"Choose your option"<<endl;
cout<<" 1) Pythagorean Triples"<<endl;
cout<<" 2) Pythagorean Theorem"<<endl;
cout<<" 3) Law of Sines"<<endl;
cout<<" 4) Law of Cosines"<<endl;
cout<<" 5) Quadratic Formula"<<endl;
cout<<" 6) Exit"<<endl;
cin>>option;
if(option==1)
PythagoreanTriples();
else if(option==2)
PythagoreanTheorem();
else if(option==3)
LawOfSines();
else if(option==4)
LawOfCosines();
else if(option==5)
QuadraticFormula();
else if(option==6)
break;

}

}
This is not finished as you can see, but i would like some input on the modules that are finished.
Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • « First
  • 3
  • 4
  • 5