mrnutty 761 Senior Poster

Easy enough. What you need to do is the following :

1) Populate the list with words from the file.
2) Start from the beginning, check if the first word is correct, i.e first letter is capitalized, and spelled correctly.
3) Check if the second word is spelled correclty, if not fix it
4) Check if the second word is now the same as the first word, if so delete it
5) Repeat until the end of the list, for each word in the list.
mrnutty 761 Senior Poster

So if I get this straight, you have a file with "pre-order" data. And your goal is to print out the data in a post order manner correct? If so then, what you could do is, take the pre-order data, store it in an array, call it PREARRAY. Then construct a tree TREE, such that preorder(TREE) = PREARRAY. Then all you would have to do now is print the TREE in postorder.

mrnutty 761 Senior Poster

Don't declare it globally. Your teacher will take points off. Instead do this:

void listmodified(Car car[], int size){
 /* some code here */
}
int main(){
 warehouse car[2];
 /* some code here */
 listmodified(car);
}

so that way you are using the same car thats in main inside of the function listmodified.

mrnutty 761 Senior Poster

Most people use C++ as base. Meaning that, they use some type of library that can handle the graphics, and use C++ to code the logic. Every time you make a project, you are making software. For example, when you make a program that checks if the string is a palindrome, you are making a palindrome checking software.

mrnutty 761 Senior Poster

@OP here is some syntax help:

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

//returns the mean of the array
double meanArray(double N1[], int size);

int main()
{
	const int MAX_DATA = 1000;
	double fileData[MAX_DATA ] = {0}; //initialize the array to contain 0
	
	ifstream fileInput("data.txt"); //open the file
        /* read data from fileInput into fileData */

	double meanValue = meanArray(fileData,MAX_DATA);

	cout<<"The meanValue = " << meanValue << "\n";
	
	return 0;


}

double meanArray(double N1[],double average){
  /* calculate the mean value from the array N1 */
}
mrnutty 761 Senior Poster

All of this could have been avoid by not using raw pointers. If you can, avoid pointers all together. If not, then use smart pointers.

mrnutty 761 Senior Poster

Problem 1 :

bool isFoo(){
 if(condition1){ return true; }
 else return false;
}

Modern compiler shouldn't complain about this. If they do, you can just ignore it.

mrnutty 761 Senior Poster

The problem you suggested should be fixed by modern compilers. Which one are you using?

mrnutty 761 Senior Poster

>>int newSize=size*=2;
OUCH! That equivalent to : size = size * 2; int newSize = size Which means that the variable size is twice its value. You wanted int newSize = size * 2 . Thus leaving the variable size unchanged.

Make sure you call delete [] expandArr at the end of main.

mrnutty 761 Senior Poster

Im not talking about the even numbers stored in array,,But im saying that the numbers which are stored on even index of array could be displayed?

Think before you speak. Thats exactly whats he talking about. Fir you need to know
how to generate even numbers such as {0,2,4...}. Then you can use those numbers as indices to your array.

Fbody commented: Thank you :) +3
mrnutty 761 Senior Poster

The first one basically reads as follows :
"Create a string object called S where S refers to the statically defined string object "abc".

The second one reads as follows :
"Create a string object called S, where S points to a new dynamically allocated string object with the value 'abc' "

Use the first one when you can.

mrnutty 761 Senior Poster

1010 in decimal =

1 * 2^3 + 0 * 2^2 + 1* 2^1 + 0*2^0 =

8 + 0 + 2 + 0 = 10

Thus 1010 in decimal is 10.

mrnutty 761 Senior Poster

Or another possibility to this stupid problem :

struct WidthProperty{
 unsigned width;
};
struct HeightProperty{
 unsigned height;
};
class Rectangle{
 WidthProperty w;
 HeightProperty h;
public:
 unsigned area(){ return w.width * w.height; }
};
mrnutty 761 Senior Poster

Pattern # 3 :

$$$$$   // Print('$', 5) && Print('5', 0) 
$$$$5   // Print('$', 4) && Print('5', 1) 
$$$55   // Print('$', 3) && Print('5', 2) 
$$555   // Print('$', 2) && Print('5', 3) 
$5555   // Print('$', 1) && Print('5', 4)
mrnutty 761 Senior Poster

>>although library implementations of std::sort usually have optimizations so it doesn't degenerate to O(n^2)).

Does that mean the stl guarantee that they use quicksort for std::sort?

mrnutty 761 Senior Poster

Sure, we would be more than glad to help you. But first there are a few things you should know :

1) We tend to help people that are "stuck on a problem" or give them a little push and whatnot. We are not here to do your homework for you.
2) Titles like "help help help" makes people not want to help you
3) Asking people for help and where you post no code and show no effort or give no description of your problem, is like trying to get your car fix, when you didn't bring it to the shop.

So next hopefully you know how to better post. Now what kind of problem are you having? Post code, and show error message, and tell us what exactly you are having trouble with. We will be more than gladly assist you, after all we are geeks.

mrnutty 761 Senior Poster

From what I hear, QT is good.

mrnutty 761 Senior Poster

Whats the error message? And all this math can be avoided by using string.

void printN(char ch, int N){
 while(N--) cout << ch;
}

int main(){

string num = "40587";

for(unsigned i = 0; i < num.size() - 1; ++i){
  if(num[i] != '0' ){
      cout << num[i] << "x1";
      printN('0',num.size() - i - 1);
      cout << " + ";
  }
 }
 cout << num[num.size()-1] << endl;

}
mrnutty 761 Senior Poster

@griswolf:

You really shouldn't have to worry about pre-optimizations. This is will not affect
the program. And for the record, if Area() is called a lot, then the function calculateArea() will be more expensive than if it were already pre calculated.

mrnutty 761 Senior Poster

>>BTW Will this also apply to the file input section

No, once you clear the flags in cin, you do not need to clear it again, unless it fails again.

mrnutty 761 Senior Poster

The point is to use the constructors. Its there exactly for this job, to initialize
things. Not using it is just a bad and redundant idea.

mrnutty 761 Senior Poster

The problem is with cin. This statement :

while (cin >> x >> y){...}

exits only when cin fails. And when cin fails, you cannot use it until you cleared its flags. Thats why cin >>oname fails.

To solve your problem, do this :

while (cin >> x >> y){
 //...
}
cin.clear(); //clear the flags
while(cin.get() != '\n'); //discard all the bad characters

for(...){...}
cin >> oname
//...and so on with the rest of your code
mrnutty 761 Senior Poster

undefined behavior.

mrnutty 761 Senior Poster

What lines does it crash in?
What is the output before it crashes?
What does the file look like?

mrnutty 761 Senior Poster

Your function delcared with a return type of double should return a double.

Here is what you should do :

double Circle::getArea(){ return _area; }
double Circle::getCircumference(){ return _circumference; }

And your area and circumference variable should already be calculated in your constructor like so :

Circle::Circle(float radius){
 _radius = radius;
 _area = PI*_radius * _radius;
 _circumference = 2*PI*_radius;
}
mrnutty 761 Senior Poster

PROGRAM IN C++ not C.

//copy linked list
	while(curr != NULL){	//Observation #1
          //...
	}
	cout << "The list was copied.";
 
	copy = curr; //Observation #2
	while(copy != NULL)//print copied linked list //Observation #3

You see what you did?

mrnutty 761 Senior Poster

PROGRAM IN C++ not C.

//copy linked list
	while(curr != NULL){	//Observation #1
          //...
	}
	cout << "The list was copied.";
 
	copy = curr; //Observation #2
	while(copy != NULL)//print copied linked list //Observation #3

You see what you did?

mrnutty 761 Senior Poster

1) Program in C++ not C.

2) Second :

//copy linked list
	while(curr != NULL){...}
 //...

copy = curr

that exits only when curr is NULL right? So when you set copy = curr, you are setting
copy to NULL essentially. Thats why your last loop isn't getting executed.

mrnutty 761 Senior Poster

You are trying to specialize a template function that does not exist!

mrnutty 761 Senior Poster

Is listNode a even template class? I ask because of this contradiction statements :

listNode<LISTDATA>*

vs

listNode *tempNode = NULL
mrnutty 761 Senior Poster

You should newtonion equation of motion.

In all your code might look something like so :

Cannon cannon;
cannon.add( Ball() );

if(keyLeftPressed){
 cannon.decreaseAngle(); 
}
else if(keyRightPressed){
 cannon.increaseAngle();
}

Vector initVelocity(5,5);

if(keySpaceBarPressed){
  cannon.shoot( initVelocity() );
}

//...
void Cannon::shoot(const Vector& initVelocity, int dt = 1){
   //x = initVelocity *dt + 1/2*acceleration * dt
   ball.updatePosition( initVelocity * dt + 1/2 * Constant::gravity * dt);
}

Haven't touched this stuff for a while, so no guarantees. I'm sure someone will come and fix something.

mrnutty 761 Senior Poster

In your inList function you are not moving your ptr. You do not even need the index and mySize. Here is something to get you stared :

bool isInList(const ElementType& elem){
 Node *curr = head;
 while curr is Not NULL{
   check if curr has the elem, if so return true;
   else advance curr to curr.next
 }
 return false;
}
mrnutty 761 Senior Poster

This what you should be doing:

int main(){
 ifstream fileInput("LinearEquations.txt"):
 if(!fileInput) return -1; //error
 //get the n value
 int numOfMatrices = 0;
 int squareSize = 0;
 fileInput >> numOfMatrices >> squareSize;
 std::vector<Matrix> matrices(numOfMatrices,Matrix(squareSize,squareSize) ):

 //read in values here in a while loop
}
mrnutty 761 Senior Poster

Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans

Its talking about when one uses the modulus operator. It is not talking about rand() in general. rand() in general is uniform.

mrnutty 761 Senior Poster

>>In general, a function can not modify the value(s) of the argument(s) used to call it

You say that then you go ahead and disprove that! Lol, what a contradiction.

@OP: some helpful things :

1) If you have a choice, use reference over pointers
2) If you do not need to change the function argument, still use reference, but make it constant qualified like so const DataType& arg0 .
3) Using reference and pointers on basic data types such as float,int,char does not make any difference in performance, therefore you can go either way, but people usually disregard the reference and pointers on basic types, unless there is a compelling reason to do so.


The main thing you should get from this is If the argument needs to be changed use reference over pointers when possible, and If the argument does not need to be changed, use const reference if possible

mrnutty 761 Senior Poster

Actually, the code suggested to OP isn't quite good. As you guys probably know the random distribution of rand() is one of the top, but usually is good enough. But by using the modular arithmetic, especially when the range is low, causes the random distribution to be even worse. So you can use the if(rand() % 2 == 0){...} as suggested, but if you want something thats a little slightly better then do something like this if(rand() > ceil(RAND_MAX/2.0f) ) {...} . The reason why this is better is because its first-half range is maximized and its second-half range is also maximized. Thus you have a bigger gap, and spread the distribution more evenly. Its just a suggestion. You don't have to follow it.

mrnutty 761 Senior Poster

Hi FirstPerson,
Thanks for reply
I haven't put every thing in a project yet. They are three seperatefiles. So it's supposed to use #include "Product.cpp"

I guess it's some other problems.

Exactly, you separated the project into files right? One a .h, .cpp and the main.cpp file, right?

So normally, one would include #include "fileName.h" where fileName is the name of the .h file, in the main.cpp. Using .cpp can cause multiple definition error. Post the error message as well.

mrnutty 761 Senior Poster

Make this change :

#include "Product.h"
#include <string>
#include <iostream>
using namespace std;
 
int main()
{
 //...
}
mrnutty 761 Senior Poster

Ok I see now. Don't be overwhelmed. Before you know it, you will be helping someone out with similar linked list problem.

So lets start :

this function at the end should be private but for testing reason make it public for now :

int LinkedList::getPredecessor(int pos){
};

Now first you want to implement this statement : "If the given position is the first place in the list,the function should return 0."

namely like so :

Node* LinkedList::getPredecessor(int pos){
 if(pos == 0)//we return NULL or 0 since there is no node before the first
   return NULL; 

 //...
};

Now you need to implement this statement :"returns a pointer to the node before that position"

your attempt is close, but this is what you have to do :

Node* LinkedList::getPredecessor(int pos){
 if(pos == 0)//we return NULL or 0 since there is no node before the first
   return NULL; 

  Node* nodeBeforePos = first; 
  for(int i = 0; i < pos - 1; ++i){
      nodeBeforePos = nodeBeforePos->next
  }
  return nodeBeforePos;
};

Lets dissect this new code.

This code : Node* nodeBeforePos = first; declares a Node variable called nodeBeforePos and sets it to point to the first node;
Then this code for(int i = 0; i < pos - 1; ++i) declares a for loop. In which i runs from 0 to pos-1. The reason why its pos-1 is because we want 1 before the pos, therefore we must stop 1 before the pos, …

bmos31 commented: Very clear, walking me through the steps was very helpful +1
mrnutty 761 Senior Poster

>> for(int i = 0; i > pos; i++)

That loop is a bug. If pos is greater than 0, then that loop will not execute. If pos is less than or equal to 0, the that loop becomes a infinite loop. What you are looking for is the "<" operator. That is i < pos . You might be getting confused a little bit.

I'm not exactly sure what your getPredecessor function does. Whats its exact purpose?

mrnutty 761 Senior Poster

or if your allowed to :

int sum = std::accumulate(resistors.begin(), resistors.end(), 0);
mrnutty 761 Senior Poster

>> for(int i = 0; i > pos; i++)

mrnutty 761 Senior Poster

Just set the y-coordinate to 0. And make x-coordinate whatever you want.

mrnutty 761 Senior Poster

The answer has to be pi?

mrnutty 761 Senior Poster

>> Imagine you wanted to count the frequency of some large input where the input has a range of values from say 0-2^32.

Right. But since we're dealing with characters, that's not the input range. ASCII's range is 0 to 127. If you're using something else, as mentioned, it's a whole different ballgame.

Cool cool, then we're on the same page. To justify myself again, I was thinking in terms of design for change, hence the usage of map.

mrnutty 761 Senior Poster

Post your code. From what I got, you should be doing something like this :

float calculateParallelValues(float resistors[], const int numOfResistors){
 //...
}
float calculateSeriesValues(float resistors[], const int numOfResistors){
}
int main(){
 const int MAX_RESISTORS = 100; //maximum number of resistors
 float resistors[MAX_RESISTORS ] = {};

 get resistor values and store them into resistors array
 ask if user want series value  
    if so call calculateSeriesValues();
 else calculateParallelValues();

  return 0;
}
mrnutty 761 Senior Poster

>>Rock(enum RockName)
You want :

Rock(RockName rockName){ myName = rockName; }
mrnutty 761 Senior Poster

Here is something to get you started :

#include <iostream>
using namespace std;

int main(){

}

Now I am not sure about the specification. Do you ask the user to input a range of Resistor values and compute its parallel or series value, or is it just 2 values?

mrnutty 761 Senior Poster

>>So if you have a big text file, or even a fairly small one that uses the majority of the letters/characters, but not all, I imagine you're using more memory in a map than an array.

The thing is, it would not. Imagine you wanted to count the frequency of some large input where the input has a range of values from say 0-2^32. Using arrays, the arrays size would have to be at least 2^32. But using map it might not be that case, that is using maps, it would only use as much as memory needed. For example for the arrays you HAVE to make its size >= 2^32 because you need to take into consideration for possibility of each elements being read. But using map you only worry about the things you read, and not worry about the things you might read.

mrnutty 761 Senior Poster

>>why not just find the count directly from the array index

I guess I was thinking in terms of "Design for change" principle. But one reason
I can give you is to save memory. For example the set {a...z} has only 26 characters,
so your array length would be at least of 26 length. So there is some space wasted there if one doesn't use up all the characters. But by using map, its length will be only the number of different characters analyzed.

So with that in mind, I realize that an array of length 26 isn't much these days. But
as I said, I was thinking of design for change, thus if one wanted to count the
frequency of say numbers instead of characters, then its a different ball game.
Because depending on the max number allowed, the array can waste a lot more memory
than map. The same goes for counting frequency for Unicode letters. And what happens if the standard decides to change the "int" value of a character? There could and couldn't be a bug if that happens.

Another reason I can think of is that using arrays, it is more error prone, as one can easily reach out of bounds exception. What happens if the user reads in say a weird value? It could lead to a bug or not. But for its simplicity, and guessing that OP is still learning, its probably an ok …