Banfa 597 Posting Pro Featured Poster

Since the ifstream variable ListNumbers is only used inside the iblock of the if statement on line 51 if you declared it on line 52 instead of line 26 then there would be no problem you will have a new file object every time the user choose to open a file.

The only addition is to reset size inside the same if block before reading the file so you fill the array in from the start again.

Banfa 597 Posting Pro Featured Poster

Doing a sort to work out something like this is excessive (although of course over a handful of numbers the extra processing is probably negligible).

A simple min routine will do the trick with the addition of an extra variable.

Initialise min and the extra variable to the first element in the array. Then scan the array looking for a new minimum value. Every time you find a new minimum value store the old minimum value in your extra variable before overwriting it with the new minimum value.

At the end of the loop the extra variable will contain the 2nd smallest value and it runs O(n) which is rather better than any sorting algorithm.

Banfa 597 Posting Pro Featured Poster

There are other issues for instance

getnumerator and getdenominator are not implemented. Also they return double then the class seems to hold the values they return as int.

You might want a mehtod that returns the fraction as a double.

You might want a method, possibly private, that simplifies a fraction.

Your operator== tests for precisely equal fractions rather than equivalence. 1/2 and 2/4 are equivalent fractions, they both actually have the value 1/2 but your operator== would return false because it compares for exactly matching denominators and numerators.

You have no way to hold whole parts of an integer. How would your class hold the value 2 3/4 (two and three quarters).

Banfa 597 Posting Pro Featured Poster

Assuming the code compiles none of it will produce the correct results because you have not correctly implemented fractional arithmetic. Take operator+

Fraction Fraction::operator+(const Fraction & fract) const
{
Fraction tempFract;
 
tempFract.num=num+fract.num;
tempFract.denom=denom+fract.denom;
 
return tempFract;
}

According to this code then

1   1   1 + 1   2   1
- + - = ----- = - = -
2   2   2 + 2   4   2

Clearly wrong since half + half = 1.

Every single one of your operators suffer from a similar problem. Get a maths book or get on the web and look up the correct algorithms for fractional arithmetic.

Banfa 597 Posting Pro Featured Poster

Sorry just saw the code, with your findMin and findMax functions you want to pass and work on a single array, like calcAvg does. The pass back the result as the return value. Don't try and print out in the functions print out in main where the functions are called.

This is generally good advice if your functions that do the calculations also do the printing then you can not reuse them in places that just require the return value. Separate the code that performs tasks from the code than prints results putting them in different functions.

Banfa 597 Posting Pro Featured Poster

You can't pass an actual array to a function but what you can do is pass a pointer to the first element of the array. In many ways a pointer to the first element can be used as an actual array for example

int array[5] = {5, 4, 3, 2, 1};
int* pointer = array;

cout << array[3] << ":" << pointer[3] << endl;

would output "2 : 2".

The important thing to remember is that when you pass an array to a function like this you loose all information about the size of the array so if it is important that the function knows that it has to be passed as well

void function2(int *ptr, int size);

void function1(void)
{
    int array[5] = {5, 4, 3, 2, 1};

    function2(array, sizeof array/sizeof array[0]);

    // array now contains 5, 4, 3, 2, 6
}

void function2(int *ptr, int size)
{
    // Set the last element of the passed array to value 6
    if (size > 0)
    {
        ptr[size-1] = 6;
    }
}
Banfa 597 Posting Pro Featured Poster

I has review it but still got error in that coding...
I don't know how to correct the error..

I've got an error is hardly a detailed description of actual program behaviour ans desired program behaviour (or a list of compiler error codes).

The more information you give the more likely it is that we will be able to help you.

Banfa 597 Posting Pro Featured Poster

So you have access to the source code of both programs?

Banfa 597 Posting Pro Featured Poster

under normal circumstances you can't do that.

You can to set up some sort of communications link (a pipe or a shared file or shared memory, or a network link) between the programs and have them co-operate with each other in executing the required functionality.

Banfa 597 Posting Pro Featured Poster

stdlib.h is one of the C standard headers and contains quite a large and varied number of function declarations, the exact contents you can easily google.

If you call a function without having first declared or defined it when it is defined somewhere in your program then in C you will get an "implicit declaration of function" warning because when the compiler sees the function call it implicitly declares the function as returning int.

#include <stdlib.h> fixes this for any functions that it contains the declarations for because if you included it then you have declared those functions so no implicit declaration happens.

You should usw #include <stdlib.h> anytime you want to call a function that it contains the declaration for.

Salem commented: Nice +20
Banfa 597 Posting Pro Featured Poster

You are more likely to get your questions answered if you actually post them.

Banfa 597 Posting Pro Featured Poster

Line 4 and 16 User_input is declared and used as accepting all double * parameters but

Line 24 void User_input (double *temp, double *mass, double *volume, double *Mmass, double Pc, double *Tc) the 5th parameter Pc is declare double not double *

Banfa 597 Posting Pro Featured Poster

Mixing cout and printf to output your results is a mistake.

That 4 being output is the return value of printf, it printed 4 characters.

Get rid of the printf and instead pipe count[c] staight to cout in the normal way. If you want to format the output read up on the io manipulators found in the header <iomanip>

Banfa 597 Posting Pro Featured Poster

Just thought I would mention the other direction. You have asked and talked about moving from the console onto GUI programming but there is another direction and that is leaving the user interface behind entirely or having a completely platform dependent interface.

C++ (and C) are used on a vast array of platforms and the console/GUI platforms you have talked about are typical of PCs (of all makes and OSes) but these actually make up the minority of platforms using C++/C code because every piece of electronic equipment you use has a processor in it running code and a lot of the time that code is compiled from C/C++. Myself I have worked on digital television receivers, 3G modems and remote data acquisition units and without exception none of them had a console let alone a GUI.

Personally I like that sort of platform. Not having to deal with the mess that generally occurs from having to have direct contact with an actual user leaves you free to concentrate on the purity of the logic required to perform the task at hand.

I'm not saying don't learn a GUI I am just trying to complete the picture of available interfaces in which none is actually rather a common one.


I would advise you to stick clear of MFC which is a bit long in the tooth, not terribly well documented, buggy and in my experience a nightmare to debug.

Banfa 597 Posting Pro Featured Poster

Don't use tabs, you will always have that problem that way. Instead use the std::setw std::left io manipulators

Banfa 597 Posting Pro Featured Poster

Have you created your own template classes BinaryHeap and PriorityQueue?

What files are the code for those classes in? Have you included them into Main.cpp?

Banfa 597 Posting Pro Featured Poster

Just a couple of notes on how you have implemented your operators. Taking operator+ and operator+= as the examples but it applies to all the other ones too.

Firstly because operator+= does in place addition it can return const Number& rather than Number which is more efficient as there is no object copy on return. Note that operator+ must return Number on the whole to work properly but that may involve copying objects (calling copy constructor and destructor) so is less efficient.

So you have implemented operator+ and implemented operator+= in terms of operator+. That's ok but consider this code

Number result, num;
int i;

// Initialise i and num

num += i;
result = num + i;
result = i + num;

Line 8 will not compile with your object.

Now instead lets pretend that you actually implemented the addition algorithm in operator+= you can write operator+ as member function like this

Number Number::operator+(const Number& number) const
{
    return Number(*this) += number;
}

This form has the advantage that a good optomising compiler can perform the return value optimisation. This optimisation removes the copy of the returned objected in some cases by constructing the return value directly in the place it is being returned to.

Better we can write the operator+ that take differently typed values as helper functions, they don't even need to be friends as they only call the public interface of Number, that allows us to write


       
jonsca commented: Good explanation +4
Banfa 597 Posting Pro Featured Poster

Line 8 of the sortQS.h you have posted recQS is a member of UList<U> but you do not call it on the object you call it as though it were a regular function try obj.recQS (0, obj.size()-1);

Banfa 597 Posting Pro Featured Poster

hi thank you very much for your help
but am a bit lost can you show me in the code please??

Not unless you show that you have some understanding of what it is you are actually trying to achieve because then I would just be doing your work for you.

You might want to start by forgetting about coding and actually doing the exercise yourself. Choose N, 4 or 5 would be good numbers, then construct the triangle yourself from spaces and stars.

Note carefully how many spaces and stars are on each line and compare that to the line number. From that build up a method of constructing each line given the value of N. Once you have done that you may be ready to start writing a computer program to implement that algorithm.


I will say that what I should have said about line 14 is

Line 14 - you use counter without initialising it to anything

which I hope makes more sense.

Banfa 597 Posting Pro Featured Poster

Line 14 - you use counter with initialising it to anything

Line 16 - you modify numOfStars modifying the end condition of the loop in line 14

Line 18 - What??? Why do you think this is required?

Line 20 - 3rd Nested Loop? You are printing out a 2D grid of spaces and * only 2 loops are required one for each dimension.

Line 24 - there is exactly 1 return per line, this should not be inside a nested loop.

Line 27 - Some lines contain more than 1 space so this should be inside a nested loop somewhere.

Line 31 - The mysterious 9999 makes another appearance for no apparent reason. If numOfStars != 9999 this is an infinite loop since numOfStars is not modified. If numOfStars == 9999 this has no effect.

Banfa 597 Posting Pro Featured Poster

If you have a single chr that is a digit then converting it to a number is easy

char achar = '5';
int theint = achar - '0'; // theint now has the value 5

If necessary validate that the char is a digit using isdigit

Banfa 597 Posting Pro Featured Poster

And how do you know that it is the code that reads the file that is wrong and not your code that writes the new file?

I see nothing in the posted code that suggests that it would not be reading the entire file only code that fails to handle the data read correctly.

Banfa 597 Posting Pro Featured Poster

How do you know it is only reading up to the NULL?

Banfa 597 Posting Pro Featured Poster

You original question question does not mention a need to do documentation. On the other hand if you mean you don't know how to look something up in your documentation how hard is it to pick up and open a book and check the index or to type something into Google.

As to the conversion from C++ to HTML the original text you posted detailed exactly what you have to do so I suggest you read what you have written.

Banfa 597 Posting Pro Featured Poster

You should check that CreateFile returns success although you are presumably getting a sensible value returned from GetFileSize.

You should check that ReadBuffer has been successfully allocated before trying to use it.

You shouldn't pass a buffer size of BufferSize+1 to ReadFile when ReadBuffer is in fact only BufferSize in length, either allocate more memory or reduce the size passed to the call.

If ReadFile is failing you should be calling GetLastError to find out what the actual error is.

However your actual problem is probably that you are trying to print as text a buffer of binary data in cout << ReadBuffer; .

That works if ReadBuffer contains text but since the nul characater '\0' is used as the end marker for text and a binary file is likely to contain lots of nul characters it only prints the file up to the first nul. You need to output the individual character values of your buffer something like cout << hex << int(ReadBuffer[0]); for the first byte in the buffer.

Banfa 597 Posting Pro Featured Poster

You need to do your own work. Start by creating a program that will copy 1 file to another a byte at a time.

Once you have that you can add filters to look for specific characters and output alternate character sequences as well as outputting extra text at the beginning and end of the file.

You will need to look up ifstream and ofstream in your documentation.

Banfa 597 Posting Pro Featured Poster

That'll teach me for quickly bashing out "a correction" when I was supposed to be concentrating on my work :D

Not only are you correct again but now I think about it when I have needed to do a similar think in my actual work I thing I have done it the way you suggest.

Banfa 597 Posting Pro Featured Poster

The reason that subject is not covered in any books on C++ is that there is no standard way to draw graphics or play sounds because C++ is a multi-platform language and graphics and sound hardware/drivers are platform specific.

You will need a library but it will have to be a 3rd party library be that QT, wxWidgets, WIN32 API or some other library. You will have to make the decision on which library to use most of the popular ones have fairly good on line documentation and tutorials.

It is not at all easy to write your own graphics and sound library.

Banfa 597 Posting Pro Featured Poster

reciprocal.hpp

extern “C” {

That should be

extern "C" {

Shift-2 not whatever it is you have put in there.

Banfa 597 Posting Pro Featured Poster

From winreg.h

#define HKEY_CLASSES_ROOT                   (( HKEY ) (ULONG_PTR)((LONG)0x80000000) )
#define HKEY_CURRENT_USER                   (( HKEY ) (ULONG_PTR)((LONG)0x80000001) )
#define HKEY_LOCAL_MACHINE                  (( HKEY ) (ULONG_PTR)((LONG)0x80000002) )
#define HKEY_USERS                          (( HKEY ) (ULONG_PTR)((LONG)0x80000003) )
#define HKEY_PERFORMANCE_DATA               (( HKEY ) (ULONG_PTR)((LONG)0x80000004) )
#define HKEY_PERFORMANCE_TEXT               (( HKEY ) (ULONG_PTR)((LONG)0x80000050) )
#define HKEY_PERFORMANCE_NLSTEXT            (( HKEY ) (ULONG_PTR)((LONG)0x80000060) )
#define HKEY_CURRENT_CONFIG                 (( HKEY ) (ULONG_PTR)((LONG)0x80000005) )
#define HKEY_DYN_DATA                       (( HKEY ) (ULONG_PTR)((LONG)0x80000006) )

You can't use #defined symbols in an enum

Banfa 597 Posting Pro Featured Poster

In your first code listing you are explicitly calling the destructors on many of your automatic scope objects, lines 18, 24, 37, 38, but that is completely unnecessary. The compiler automatically adds destructor calls for these objects so you destruct them twice. Since that is likely to involve a delete call it is a receipe for disaster.

In your most recent code listing you delete coefs without zeroing it, test the pointer value of coefs and then deleteit again line 53 - 58.

You should not be deleting an object more than once. Anything else is undefined behaviour (or a memory leak if you don't delete it at all).

Line 56, how can this be anything other than true? If it is false you have a problem because you have 2 polynomial objects using the same data buffer.

On the whole I think your entire class would be simpler if you just used vector<double> coef rather than double* coefsince it would perform the memory management for you.

And finally this for(int i=base.degree;i>=0;i--) is quite a strange way to construct a for loop that is not required to go backwards (minor note and would be an infinite loop if you used unsigned variables).

Normall most people code for loops as for(int i=0;i<base.degree;i++) unless there is a specific need to go backwards through the array. if you used a vector you could just use a reverse iterator if required.

Banfa 597 Posting Pro Featured Poster

I think the equation I += I % 14 is wrong. For example if I == 15, the statement will add 1, which does not make it a multiple of 14.

Your completely right it should of course be

I += 14 - (I % 14)

Which is the equivilent of your statement

Banfa 597 Posting Pro Featured Poster

You don't use the the output from the stringstream sa to resize your query table.

That is what everyone has been saying. Your query table has a fixed size, that is the number of defined questions, 300 from your first post.

You have 2 options either you select question randomly as you ask them. In this case you need a table with an entry for every question (300) which you use to initially mark all questions as unused and as you ask a question you mark it as used so that if it gets selected again then you can make a new selection rather than asking the same question.

Another option is before asking any questions build a list of the question you will ask that references the complete table of questions.

Ignoring possible problems if the number of questions being asked is a large proportion of the number of available questions those 2 different method both require an additional table that is not the query table.

the query table remains a fixed size it is the ancilary tables whose size may alter depending on the method used to record questions asked/to ask.

dualdigger commented: really helpful suggestions +1
Banfa 597 Posting Pro Featured Poster

Sorry I missed the even bit, still no loop required. Even means divisible by 2 divisible by 2 and divisible by 7 means divisible by (2 * 7) = 14

Replace the step

Find the first integer >= I that is a multiple of 7, I += I % 7

with

Find the first integer >= I that is a multiple of 14, I += I % 14

Banfa 597 Posting Pro Featured Poster

Why would you need a loop.

Find the cube root of 40,000 pow(40000, 1.0/3.0) call it R
Find the first integer > R, I = (int)R + 1
Find the first integer >= I that is a multiple of 7, I += I % 7

BTW you have written pow(40000, 1/3) , 1/3 is integer arithmetic and equals 0 anything raised to the power 0 is 1 (except may be 0) so pow(40000, 1/3) = 1. That is certainly causing your loop logic to be wrong.

Banfa 597 Posting Pro Featured Poster

Line 46 you have an unmatched { remove it.

Banfa 597 Posting Pro Featured Poster

You will need to change this line as well m_Qtable.resize(m_QNo); as well and in fact you will need to change all instances of m_QNo in your code, both posted and unposted where you have accidentally used the question number rather than the number of available questions as your limit for accessing the question table.

Ancient Dragon commented: Yes, your right. +28
Banfa 597 Posting Pro Featured Poster

You never assign anything to the variable totAmt.

The solution you are looking for is stated in the actualy question text you have posted

Your program should pass these 3 arrays to a function named calc()

You are only passing 2 of those 3 arrays. Pass the 3rd one and you can fill it in. You do not need the total temporary variable in calc.

Banfa 597 Posting Pro Featured Poster

Did you seed the random number generator with srand() ?

Banfa 597 Posting Pro Featured Poster

Firstly your iterative version is highly in efficient. If you checked a != b between lines 10 and 11 then you could avoid all the loops and other checking when a == b. That is quite a lot of processing because you will cut out 9 * 9 * 9 * 9 = 6591 iterations of themain loop body.

You will need to include more { } pairs to properly block your loops.

Also notice I said 9 * 9 * 9 * 9 not 10 * 10 * 10 * 10? Your algorithm is wrong. Have you checked the output? Not a single number will contain the digit 9 because you have all your loop end condition wrong.

In your recursive version your method to isolate the digits does not work, consider the number 123456 and consider your attempt to isolate the 3rd digit n / 1000. 123456 / 1000 = 123. It clearly doesn't work. You need to use the % operator.

Also you have a while loop in the function. A loop like that in a supposedly recursive function is normally a clear sign of an error. recursion replaces iteration.

A recursive function should have an end condition. If the end condition is met the function returns otherwise the function recurses.

The whole recursion returns a single result. Your cout statement is outside the function in main so if the function was working the program would pront a maximum of 1 …

Banfa 597 Posting Pro Featured Poster

I also could not find info about atod. It seems that there is not such a function in stdlib.h.
atof does the job but the precision is up to 6 digits after point(i.e. up to 0.123456 is shown and longer digits are rounded up: 0.123457).

No it should give 15 digits of precision or 14 after the point assuming 1 before the point.

May be this is the cause of the confusion, atof returns double not float so there is no atod as it would be superfluous.

Banfa 597 Posting Pro Featured Poster

The problem is not in how you are declaring client it is how you are doing the construction of the new object. The missing or empty parenthesis indicate that the default constructor of Sftp should be called but the compiler can not find a default cinstructor for that class. Presumably the class is defined with a constructor that require parameters. You must pass these parameters when creating the object

Sftp^ client = gcnew Sftp([b]<Construction Parameters Here>[/b]);
Banfa 597 Posting Pro Featured Poster

No.

If you have a derived class object DO and call DO.m then only DO.m is executed. But if you call the destructor both the derived class destructor and the base class destructor get called (in that order).

Banfa 597 Posting Pro Featured Poster

Dear Banfa and Jephthah,
Thank you so much for the useful answers.
I made the changes except :
because when I change atoi to atod, it shows:
With atoi it runs well and shows integers in the output but I need to store double floating numbers in the array.

When you change your code from doing the wrong thing to doing the right thing and you get errors in compilation the correct course of action is not to change back to doing the wrong thing but to understand and fix the source of the errors.

You do not need line 7 of your lastest listing as atof is declared in stdlib.h. In fact it is very poor style to re-declare a standard library function like this.

If you want to convert floating point numbers you will need to call atof.

Banfa 597 Posting Pro Featured Poster

Yes

Banfa 597 Posting Pro Featured Poster

it wont compile, but shows no error messages either

No either it compiles in which case it will show no errors but you may get warnings which don't stop it compiling or if fails to compile in which case you get at least 1 error.

As it happens your code compiles for me with a single warning.

I suggest you may need to give us more detail of what you are doing and what the result is and what you expected the result to be.

Banfa 597 Posting Pro Featured Poster
List<String^> Lines = gcnew List<String^>();

Declares an object not a handle and initialises it to an empty list that you gcnew to. However since it is an object and not a handle when you try to assign to it you get an operator= unavailable error because the class List does not implement operator=

If you want to be able to assign new allocated lists to the variable declar it as a handle

List<String^>^ Lines = gcnew List<String^>();
Lines = gcnew List<String^>();
Banfa 597 Posting Pro Featured Poster
while(fgets(line, 100, file)!=NULL) {       /* keep looping until NULL pointer... */

"I have detected danger Will Robinson"

More specifically I have detected that line was declared char line[20]; so is a 20 byte array but you have passed a pointer to line and told fgets to copy up to 100 characters into it leaving yourself open to a buffer overrun. For safety this should be

while(fgets(line, sizeof line, file)!=NULL) {       /* keep looping until NULL pointer... */

And again

numbers[i]=atoi(line); /* convert string to int */

but numbers is declared double numbers[]={0}; with no array size but an initaliser list with a single entry means that numbers has a size of 1 so as soon as i > 0 you have an out of bounds array access.

Additionally your file appears to contain floating point values and you are using double type to store your converted value but you are calling int atoi ( const char * str ); which returns int and explains many of your conversion errors. You should try calling double atof ( const char * str ); .

To get your numbers array the right size you are going to have to do something using dynamic memory allocation with malloc/realloc.

Salem commented: Yes +20
Banfa 597 Posting Pro Featured Poster

No intialisation lists only work on constructors which are special functions for initialising the object and they specifically initialse member variables and sub-classes rather than assigning to them.

Initialisation only happens when the object is being created so you can initialise in a member function because the object is not being created it already exists you have to assign to it.

Banfa 597 Posting Pro Featured Poster

int (**p1)[4] = &p;