C/C++ Thread

Cromewell

Administrator
Staff member
It's hard to say, they are probably asking you to use the get function. I think this will work as a starting point (should read until a newline character is found)
Code:
char c_input;
bool b_keepReading = true;
while(b_keepReading){
 cin.get(&c_input);
 if (c_input == "\n"){
  b_keepReading = false;
 }
}
 

brian

VIP Member
I just used the getch() function, It worked. Since they were so vague, I think it will work. Thanks for the hints.
 

mihir

VIP Member
Nice?


I wanted to know have you guys been taught about time complexity and space complexity and stuff in languages
 

Dystopia

Active Member
Ok guys, need some help here. I want to turn in this assignment by the latest Wednesday.

I need to write a program that reads numbers out of a .dat file, into an array. Then it needs to find the standard devation, range, median, and average. I've got the code for the average done, thats easy. I'd need help with the code on the deviation. And, I need help on sorting the numbers. I cannot figure out how to sort the numbers from lowest to highest (or any way, for that matter). No matter what I try, no dice. I did look in my book, but I can't really do it the way they show it, as they are not taking the numbers in the arrays.

So, to clarify, I need to figure out how to sort the numbers. And still be able to access them of course. Here is the code I have, which does not work. No errors, just a BS number (like -9 trillion or something):

Code:
//sort.cpp sorts the index from lowest value to highest value
//
//Adam Holthaus

#include "stdafx.h"
#include <iostream>
using namespace std;

double sort(double index[], int esave)
{
	//Initializing variable
	int n, min, entry, edefine;


	for(edefine = 0; edefine < esave; edefine++)
	{
		min = index[edefine];
		n = edefine;

		for(entry = edefine; entry < esave; entry++)
		{
			if(index[n] < min)
			{
				min = index[n];
				n = entry;
			}
		}

		index[n] = index[edefine];
		index[edefine] = min;
	}

	return index[esave];
}

esave stands for the amount of numbers in the array. I kept track reading out of an array. index[] is my array, which already has the numbers in it. You can see what I did in the code...what's wrong.

I think it might be easiest if you made your own .dat file or .txt file.

Thanks guys.
 

Cromewell

Administrator
Staff member
Ok guys, need some help here. I want to turn in this assignment by the latest Wednesday.

I need to write a program that reads numbers out of a .dat file, into an array. Then it needs to find the standard devation, range, median, and average. I've got the code for the average done, thats easy. I'd need help with the code on the deviation. And, I need help on sorting the numbers. I cannot figure out how to sort the numbers from lowest to highest (or any way, for that matter). No matter what I try, no dice. I did look in my book, but I can't really do it the way they show it, as they are not taking the numbers in the arrays.

So, to clarify, I need to figure out how to sort the numbers. And still be able to access them of course. Here is the code I have, which does not work. No errors, just a BS number (like -9 trillion or something):

Code:
//sort.cpp sorts the index from lowest value to highest value
//
//Adam Holthaus

#include "stdafx.h"
#include <iostream>
using namespace std;

double sort(double index[], int esave)
{
	//Initializing variable
	int n, min, entry, edefine;


	for(edefine = 0; edefine < esave; edefine++)
	{
		min = index[edefine];
		n = edefine;

		for(entry = edefine; entry < esave; entry++)
		{
			if(index[n] < min)
			{
				min = index[n];
				n = entry;
			}
		}

		index[n] = index[edefine];
		index[edefine] = min;
	}

	return index[esave];
}

esave stands for the amount of numbers in the array. I kept track reading out of an array. index[] is my array, which already has the numbers in it. You can see what I did in the code...what's wrong.

I think it might be easiest if you made your own .dat file or .txt file.

Thanks guys.
Do you have to write your own sorter function? qsort is built into cstdlib and is quite good at what it does. edit: Here's a simple insertion sort, you can find the algorithm all over the place.
Code:
void sort(double d_array[], int i_arrSize)
{
    int i_swap;
    double d_temp;
    
    for (int x = 0; x < i_arrSize; x++){
        d_temp = d_array[x];
        i_swap = x-1;
        
        while(d_temp < d_array[i_swap] && i_swap >= 0){
            d_array[i_swap+1] = d_array[i_swap];
            i_swap--;
        }
        
        d_array[i_swap+1] = d_temp;
    }
}
/edit
Can you explain this function to me, why are you returning the last element of the array?
[-0MEGA-];1610703 said:
Code:
<echo>
Hello World!
</echo>
That's the strangest C++ I have ever seen.... :p
 
Last edited:

mihir

VIP Member
Ok guys, need some help here. I want to turn in this assignment by the latest Wednesday.

I need to write a program that reads numbers out of a .dat file, into an array. Then it needs to find the standard devation, range, median, and average. I've got the code for the average done, thats easy. I'd need help with the code on the deviation. And, I need help on sorting the numbers. I cannot figure out how to sort the numbers from lowest to highest (or any way, for that matter). No matter what I try, no dice. I did look in my book, but I can't really do it the way they show it, as they are not taking the numbers in the arrays.

So, to clarify, I need to figure out how to sort the numbers. And still be able to access them of course. Here is the code I have, which does not work. No errors, just a BS number (like -9 trillion or something):

Code:
//sort.cpp sorts the index from lowest value to highest value
//
//Adam Holthaus

#include "stdafx.h"
#include <iostream>
using namespace std;

double sort(double index[], int esave)
{
	//Initializing variable
	int n, min, entry, edefine;


	for(edefine = 0; edefine < esave; edefine++)
	{
		min = index[edefine];
		n = edefine;

		for(entry = edefine; entry < esave; entry++)
		{
			if(index[n] < min)
			{
				min = index[n];
				n = entry;
			}
		}

		index[n] = index[edefine];
		index[edefine] = min;
	}

	return index[esave];
}

esave stands for the amount of numbers in the array. I kept track reading out of an array. index[] is my array, which already has the numbers in it. You can see what I did in the code...what's wrong.

I think it might be easiest if you made your own .dat file or .txt file.

Thanks guys.
You cannot return arrays like that.
You can try writing your own quick sort.Since it is the second fastest sorting algorithm(excluding radix sort) and also the most space efficient.
And if space is not a constriction then a merge sort would do.
Have you been given a time complexity or not.If not then even a bubble sort would do.
Code:
<echo>
Hello World!
</echo>
Thats HTML
 

Cromewell

Administrator
Staff member
You cannot return arrays like that.
You can try writing your own quick sort.Since it is the second fastest sorting algorithm(excluding radix sort) and also the most space efficient.
And if space is not a constriction then a merge sort would do.
Have you been given a time complexity or not.If not then even a bubble sort would do.

I wouldn't suggest a bubble sort even for the simplest array of data. I'm hoping it's not even taught in schools anymore. A selection sort is way more efficient and not very difficult to implement and the insertion sort (that I posted above) is a little faster still.
 
Last edited:

Dystopia

Active Member
You cannot return arrays like that.
You can try writing your own quick sort.Since it is the second fastest sorting algorithm(excluding radix sort) and also the most space efficient.
And if space is not a constriction then a merge sort would do.
Have you been given a time complexity or not.If not then even a bubble sort would do.
Code:
<echo>
Hello World!
</echo>
Thats HTML
Pointing out the fact that I was returning the array the way I was probably fixed it, let me see. What do you mean with a quick sort? Or merge sort? Time is not an issue, neither is space. From what I saw, the bubble sort doesn't seem like it will work well for this.
Do you have to write your own sorter function? qsort is built into cstdlib and is quite good at what it does. edit: Here's a simple insertion sort, you can find the algorithm all over the place.
Code:
void sort(double d_array[], int i_arrSize)
{
    int i_swap;
    double d_temp;
    
    for (int x = 0; x < i_arrSize; x++){
        d_temp = d_array[x];
        i_swap = x-1;
        
        while(d_temp < d_array[i_swap] && i_swap >= 0){
            d_array[i_swap+1] = d_array[i_swap];
            i_swap--;
        }
        
        d_array[i_swap+1] = d_temp;
    }
}
/edit
Can you explain this function to me, why are you returning the last element of the array?

That's the strangest C++ I have ever seen.... :p

Yeah, I have to write my own (but I'm allowed to get help). Why all the underscores in the one you posted? Just confuses me :p

I don't know why I am returning the index with the last element...I'm going to fix that, and try it again, thanks.

EDIT: Didn't fix it.

Anyone know what I should be returning?

Also, thanks for the code Cromewell, I'm using it. Will update later.

EDIT2: still not working...
 
Last edited:

Cromewell

Administrator
Staff member
Pointing out the fact that I was returning the array the way I was probably fixed it, let me see. What do you mean with a quick sort? Or merge sort? Time is not an issue, neither is space. From what I saw, the bubble sort doesn't seem like it will work well for this.


Yeah, I have to write my own (but I'm allowed to get help). Why all the underscores in the one you posted? Just confuses me :p

I don't know why I am returning the index with the last element...I'm going to fix that, and try it again, thanks.

EDIT: Didn't fix it.

Anyone know what I should be returning?

Also, thanks for the code Cromewell, I'm using it. Will update later.

EDIT2: still not working...

You don't need to return anything. The array is passed as a pointer. The underscores are there because I prefix the variable name with the type, it's just a habit. The only place I don't do it is in for loop counters.

Here's the full test framework I was using for it. It's working here.
Code:
//sort.cpp sorts the index from lowest value to highest value
//
//Adam Holthaus

#include <iostream>
#include <cstdlib>
#include <cstdio>

void sort(double, int);

using namespace std;

void sort(double d_array[], int i_arrSize)
{
    int i_swap;
    double d_temp;
    
    for (int x = 0; x < i_arrSize; x++){
        d_temp = d_array[x];
        i_swap = x-1;
        
        while(d_temp < d_array[i_swap] && i_swap >= 0){
            d_array[i_swap+1] = d_array[i_swap];
            i_swap--;
        }
        
        d_array[i_swap+1] = d_temp;
    }
}

int main(){
    double d_sortMe[] = {1.0,2.0,5.0,3.0,6.0,2.5,1.3};
    int i_arrSize = sizeof(d_sortMe)/sizeof(double);

    printf("Original Order\n");
    printf("------------------------\n");    

    for (int x = 0; x < i_arrSize; x++){
        printf("%lf\n",d_sortMe[x]);
    }

    printf("------------------------\n");
    printf("Sorted\n");
    printf("------------------------\n");
            
    sort(d_sortMe, sizeof(d_sortMe)/sizeof(double));
    
    for (int x = 0; x < i_arrSize; x++){
        printf("%lf\n",d_sortMe[x]);
    }

    printf("------------------------\n");
    
    system("pause");
    
    return 0;
}
 

Dystopia

Active Member
Is that in C? Because in for us, in C++, to print something to a screen, we use

Code:
cout << "whatever you want to say";
 

Cromewell

Administrator
Staff member
It's a little mixed. This code produces the exact same behaviour. Just changed the printfs to couts.
Code:
//sort.cpp sorts the index from lowest value to highest value
//
//Adam Holthaus

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>

void sort(double, int);

using namespace std;

void sort(double d_array[], int i_arrSize)
{
    int i_swap;
    double d_temp;
    
    for (int x = 0; x < i_arrSize; x++){
        d_temp = d_array[x];
        i_swap = x-1;
        
        while(d_temp < d_array[i_swap] && i_swap >= 0){
            d_array[i_swap+1] = d_array[i_swap];
            i_swap--;
        }
        
        d_array[i_swap+1] = d_temp;
    }
}

int main(){
    double d_sortMe[] = {1.0,2.0,5.0,3.0,6.0,2.5,1.3};
    double d_stdDev;
    int i_arrSize = sizeof(d_sortMe)/sizeof(double);

    cout << "Original Order" << endl;
    cout << "------------------------" << endl;

    for (int x = 0; x < i_arrSize; x++){
        cout << d_sortMe[x] << endl;
    }

    cout << "------------------------" << endl;
    cout << "Sorted" << endl;
    cout << "------------------------" << endl;
            
    sort(d_sortMe, i_arrSize);
    
    for (int x = 0; x < i_arrSize; x++){
        cout << d_sortMe[x] << endl;
    }

    cout << "------------------------" << endl;
    
    system("pause");
    
    return 0;
}
 

Dystopia

Active Member
Ok, new problem. We have been working on overloading operators. And all as went well until THIS project.

I need to add two fractions, with different denominators. I got the LCD part figured out (actually, I figured that out while posting this and saying "wait a sec" to myself). This is how I am doing the math part (mentally), using these fractions:

1/6 + 7/9 = ?/?

LCD of 6 and 9 = 18

1/6 * 3/3 = 3/18

7/9 * 2/2 = 14/18

Now I can add

3/18 + 14/18 = 17/18

I cannot figure out how to tell the program to multiply the way I did. Obviously I could do that if I wanted, but then I could not enter different values. The values are whatever I put in, I chose the ones that I listed. Here are the function codes:

LCD (works fine for me):

Code:
long rational::lcd(long d2)
{
	long minden, maxden, multiple;
	minden = min(den, d2);
	maxden = max(den, d2);
	multiple = 1;
	while(maxden*multiple%minden !=0)
		multiple++;
	return maxden*multiple;
}

GFC code (I have no idea what to use for what):
Code:
long rational::greatestcommonfactor(long x1, long x2)
{
	long minnum, maxnum, gcf;
	minnum = min(abs(x1), abs(x2));
	maxnum = max(abs(x1), abs(x2));
	gcf = minnum;
	while(maxnum % gcf !=0 || minnum % gcf !=0)
		gcf--;
	return gcf;
}

overloaded operator + code (this is working fine for me):
Code:
rational rational:: operator +(rational fraction_2)
{
	long cden, gfc, x1, x2;
	rational sum;		

	cden =  lcd(fraction_2.den);

	sum.den = cden;

	x1 = num;
	x2 = fraction_2.num;

	gfc = greatestcommonfactor(x1, x2);

		
	sum.num = x1 + x2;

	return sum;
}

Here is my int main. you need to replace my int_main line with yours:
Code:
// lab20.cpp : Defines the entry point for the console application.
//


#include "rational.h"

int _tmain(int argc, _TCHAR* argv[])
{
	rational fraction_1, fraction_2, sum;

	cout <<"Please enter fractions seperated by spaces, numerator and denominator seprated by spaces: ";
	cin >>fraction_1 >>fraction_2;

	sum = fraction_1 + fraction_2;

	cout <<fraction_1 <<endl <<fraction_2 <<endl <<sum <<endl;
	return 0;
}

and in case you want it, the .h file for the rational class:
Code:
// header file to define the rational class
//
//Adam Holthaus, steve05

#include <iostream>
#include <fstream>
using namespace std;

class rational
{
	friend istream& operator >>(istream&, rational&);
	friend ostream& operator <<(ostream&, rational);

private:
	long num, den;

public:
	void reduce();
	long greatestcommonfactor(long, long);
	long lcd(long);
	rational operator +(rational);
};
 

Cromewell

Administrator
Staff member
Ok, new problem. We have been working on overloading operators. And all as went well until THIS project.

I need to add two fractions, with different denominators. I got the LCD part figured out (actually, I figured that out while posting this and saying "wait a sec" to myself). This is how I am doing the math part (mentally), using these fractions:

1/6 + 7/9 = ?/?

LCD of 6 and 9 = 18

1/6 * 3/3 = 3/18

7/9 * 2/2 = 14/18

Now I can add

3/18 + 14/18 = 17/18

I cannot figure out how to tell the program to multiply the way I did. Obviously I could do that if I wanted, but then I could not enter different values. The values are whatever I put in, I chose the ones that I listed. Here are the function codes:

I've been trying but I can't get your code to build, getting a ton of errors. But the general process should be:
Find lowest common denominator.
Find what you need to multiply the fractions by to make them have that denominator. (Divide lowest common denominator by the current denominators)
Multiply the numerators by the respective numbers from the previous step.
Add the numerators together, the new numerator goes over the lowest common denominator.
Reduce your new fraction.
 

Dystopia

Active Member
I've been trying but I can't get your code to build, getting a ton of errors. But the general process should be:
Find lowest common denominator.
Find what you need to multiply the fractions by to make them have that denominator. (Divide lowest common denominator by the current denominators)
Multiply the numerators by the respective numbers from the previous step.
Add the numerators together, the new numerator goes over the lowest common denominator.
Reduce your new fraction.

That is the only issue I am having right now. The LCD function returns the LCD though, so I won't know what numbers are multiplying. You've given me a couple ideas though, so I'll try them and get back to you. Thanks.
 

NyxCharon

Active Member
If you have a fairly good grasp of java overall, what would be the best way to begin learning c/c++? What's the best source/book/etc for someone who already understands a language?
 

Dystopia

Active Member
Try using Starting Out With C++: From Control Structures through Objects - 6th edition by Tony Gaddis

Great book, I really like it.
 

Troncoso

VIP Member
Try using Starting Out With C++: From Control Structures through Objects - 6th edition by Tony Gaddis

Great book, I really like it.

Yep that's actually the same book I used for my class. considering my class was online I learned everything I know about C++. from that book.
 

kobaj

VIP Member
Got a C++ DLL question for you guys. (No doubt this is something simple I'm overlooking.) Anyway, I'm making a DLL following this guide here. And a mix of this nifty code here in my header.

Now the problem comes when importing/referencing/trying to use the DLL I've compiled in a different solution than the one the DLL is in. All the guides I read say to reference a .lib file, but I don't seem to have this file. With a bit of reading, I can make a .lib file (static library as I'm told, ie, a new/different project than the dll project) but then the .dll isn't made.

So am I doing something wrong? (or do you guys need more info the help me out?)
 
Top