Thread: ALL NEWBIES, and even advanced people, lets do this!

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    ALL NEWBIES, and even advanced people, lets do this!

    Ok, I'm sure there has got to be newbies like me somewhere out there reading this. I'm going through the tutorials and I want to eventually be through all of them. I just finished the 4th one (functions-->cool, hehe). Here is what I have so far that uses stuff from the first 4 tutorials (except loops):

    #include <iostream.h>
    #include <conio.h>
    #include <windows.h>
    #include <stdlib.h>
    int avg(int x, int y, int n);

    int main()
    {
    cout<<"This program will get the average of numbers you enter."<<endl;
    int x, y, n;
    cout<<"Please enter the scores to be averaged (use a space in between numbers): ";
    cin>>x>>y;
    cout<<"Enter how many scores you entered: ";
    cin>>n;
    cout<<"The average of you numbers is: "<<avg(x, y, n)<<flush;
    _getch();
    return 0;
    }
    int avg(int x, int y, int n)
    {
    return (x+y)/n;
    }

    now you other newbies show me what you got too! And advanced people show us what we will be able to do eventually!

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    This is probably the most useful, and biggest thing i've done in C++, some aspects of it are unfinished.

    Code:
    #include <math.h>
    #include <conio.h>
    #include <iomanip>
    #include <string>
    #include <fstream.h> 
    
    // This program finds the standard deviation of a set of data.
    // 1: Mean
    // 2: (Data Member - mean) * Data Member.
    // 3: Mean of data from 2.
    // 4: Square root of numbers from 3.
    
    int main()
    {
    	float temp = 0;
    	int num;
    	int stupid = 0;
    	cout<<"How many instances of data do you want to enter? ->";
    	cin>>num;
    	while (num < 0)
    	{
    		cout<<"You must enter a positive integer, please do so now ->";
    		cin>>num;
    		stupid++;
    		if (stupid == 5)
    		{
    			cout<<"Are you really that retarded?\n\n";
    			return 0;
    		}
    
    	}
    	cout<<"\n--------------------------\n";
    	float *arguments = new float[num];
    	for (int i=0; i<num; i++)
    	{
    		cout<<"Enter data member number "<<i+1<<"-> ";
    		cin>>arguments[i];
    		temp = temp + arguments[i];
    //		if (arguments[i] < 0) // Fix negitive number squaring problem
    //		{
    //			double negitivefix = arguments[i];
    //			arguments[i] =- arguments[i];
    //			arguments[i] =- negitivefix;
    //		}
    		ofstream fout; 
    		fout.open("C:\\file.txt"); //Open file
    		fout <<arguments[i]<<"\n";  //Output data to file
    		fout.close(); 
    
    	}
    	float mean = temp / num;
    	temp = 0;
    	cout<<"\n\nThe mean(average) of your data = "<<mean<<"\n";
    	cout<<"\n--------------------------\n";
    	for (i=0; i<num; i++)
    	{
    		arguments[i] = arguments[i] - mean;
    		arguments[i] = pow(arguments[i],2);
    		temp = temp + arguments[i];
    		cout<<"Squared deviation of data member "<<i+1<<" = "<<arguments[i]<<"\n";
    	}
    	float devmean = temp / num;
    	float squareroot = sqrt( devmean );
    	cout<<"\n\n";
    	cout<<"The mean of the squared deviations = "<<devmean;
    	cout<<"\nStandard Deviation of your data = "<<squareroot<<"\n\n";
    	delete[] arguments; // Clean up allocated memory
    	short int stoprog; // Pause the program so the user can read the output
    	cin>>stoprog;
    	return 0;
    }

  3. #3
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    Yes...

    Ahh, I'm guessing you are in Algebra 2 right now? Deviations and stuff.

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Well yes, we're doing that in school right now, Im not sure if its called algebra 2 around here. I'll be making more programs to do my homework for me

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    In regards to the statistics:

    Isn't variance (what you have called "mean square deviation") supposed to be the sum of the squared differences divided by (n-1) not n?

    In regards to the last program I did, this is it. It is a single neuron following the perceptron model, to do pattern classification of inputs:

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <conio.h>
    #include <fstream.h>
    
    double w[3] = {1,1,1}; // some inital weights
    double mu = 0.2;  // learning rate
    
    int CalculateValue(int x1, int x2);
    void UpdateWeights(int error, int x1, int x2);
    
    int main(){
    
    	bool finished;
    	int error, count = 0;
    	int trainingData[4][3] = {{-1,1,1},{0,0,0},{-1,-1,1},{1,0,0}};
    	do{
    		finished = true;
    		for (int i = 0; i < 4; i++){
    			count++;
    			error = trainingData[i][2] -
    				CalculateValue(trainingData[i][0],trainingData[i][1]);
    			if (error != 0){
    				finished = false;
    				UpdateWeights(error,trainingData[i][0],trainingData[i][1] );
    			}
    		}
    	}while(!finished);
    	ofstream outStream("prob5o.txt");
    	outStream << "Results: " << endl;
    	for (int i = 0; i < 3; i++){
    		outStream << "w[" << i << "] = " << w[i] << endl;
    	}
    	outStream << endl << "Total, we performed " << count << " iterations";
    }
    
    int CalculateValue(int x1, int x2){
       // new variables so we don't have to worry about casting/mixed type math.
    	double d_x1 = x1;
    	double d_x2 = x2;
    
    	double intermediate = w[0] + w[1]*d_x1 + w[2] * d_x2;
    	//intermediate is our net function
    	//We use a step activation function
    	if (intermediate >= 0) return 1;
    	return 0;
    }
    
    void UpdateWeights(int error, int x1, int x2){
    	double d_error = error;  // same reason as before, make doubles
    	double d_x1 = x1;
    	double d_x2 = x2;
    
    	w[0] = w[0] + mu * d_error;
    	w[1] = w[1] + mu * d_error * d_x1;
    	w[2] = w[2] + mu * d_error * d_x2;
    }

  6. #6
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    Mine would have to be my ASMEDit IDE I'm writing (win32) that or my AquaEdit I'm writing for someone (win32 MDI) too much to post...

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    79
    Dividing by (n-1) instead of n yields the sample standard deviation instead of the standard deviation for a population. The latter is denoted by a sigma, while the former is denoted by an 's'.
    All generalizations are false

  8. #8
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    sample variance is denoted by
    s^2
    sample standard deviation is denoted by
    s
    which is the square root of the sample variance
    sigma squared is the population variance
    sigma is the population standard deviation

    You divide by n in the population variance, and
    you divide by n-1 in the sample vaiance.

    This is because the sample mean is not the real mean, thus the variation is greater. This much I understand, but I'm not sure why it's one and not .5 though, maybe they found it doing empiratical studies or they proved it some how?.

  9. #9
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    this is it...

    Ok, variance is the mean of the squares of the deviations from the mean. The deviation of the mean is the difference between the data item and the mean. standard deviation is the principal square root of the variance. and yes, it is denoted by sigma.

  10. #10
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    you boys having fun with your m...m...m...I can't say it...m...mmma.....th...math. UGH! *pukes repeatedly*

Popular pages Recent additions subscribe to a feed