Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Start out by posting in Game Development forum. How well do you know c language? Or any other programming language ?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

It makes sense that the squart root of a negative number is an imaginary number. You can not multiply two negative numbers and get a negative number. For example sqrt(-25) = -5?? But -5 * -5 != -25.You probably should have learned that in 5th grade math, or certainly high school algebra.


>>example and the user input -624.5 and answer resulted as -24.99.
Impossible. The squart root of 625.5 is indeed 24.99, but not -624.5.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The square root of a negative number is an imaginary number. sqrt(-1) is not -1. To confirm this, see this calculator

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I think it would be a lot simpler to use getline() to read an entire line, then parse its contents. With this you don't have to worry about unget().

string line;
while( getline(infile, line) )
{
    // parse the string here, similar to the way you did
    // it in your original program.

}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you're wrong. There are a lot of people who are actually allergic to apples.
Some like me to a very mild degree, we just get diarrhea eating raw apples. Others need medical attention.

I love apples, but eating more than one or two a day can produce diarrhea for a day or so. Why? Not because of the seeds but because they are excellent source of fiber.

Their seeds are, because they contain cyanide. It is a low amount, but eating a ton of apple seeds could harm you.

I know of no one who eats the seeds intentionally.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I agree with your teacher. TurboC is very ancient. Did your teacher suggest a compiler? If not, there are several of them you can use
Code::Blocks with MinGW compiler
VC++ 2008 Express

just google for those names and you will find their download links.

But what does that have to do with the subject of your thread?? You didn't mention ncurses at all.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There are several ways to solve that problem. IMHO this is the simplest.
sprintf() is your friend here

char filename[255]; // final file name
 char* namePt1 = "Labyrinth";
int num = 5;
sprintf(filename,"%s%d.txt", namePt1,num);

ofstream fileLabyrinth(filename);

Alternate method is pure c++

string filename;
string namePt1 = "Labyrinth";
int num = 5;
filename = namePt1;
stringstream str;
str << num;
filename += str.str();
filename += ".txt";

ofstream fileLabyrinth(filename.c_str());
DSalas91 commented: He help me very well! :) +0
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The four bytes before the beginning of the BSTR is the number of bytes allocated to the BSTR, which may or may not be the length of the string. In fact the BSTR need not contain a null-terminated string at all (either UNICODE or ascii). It could contain binary data of any type. It's up to your program to interpret the contents of the BSTR correctly.

In the example code you posted, the value of 34 is correct because 17 * sizeof(wchar_t) is 17 * 2 = 34. Note that in this case the string is NOT null terminated because you didn't tell SysAllocStringLen() to include that. If you wanted the null terminator then you have to add 1 to the length of the string.

Run the same program on *nix and you will probably find different results because sizeof(wchar_t) on MS-Windows (2) is different then it is on *nix (4). So if you transmit that BSTR from MS-Windows to *nix (or vice versa) then some sort of translation will need to be made.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

But anyway what would be the question to the interview question? I found in some forum posting real interview questions (I guess it is asked by bloomberg which asks all sorts of peculiar c questions)

Oh I see -- its one of those questions someone asks to see if you really know your stuff.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

See my answer in your other thread.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The functions isalpha() and isupper() will tell you whether the character is an upper-case character or not

for(int i = 0; i < userInput.length(); i++)
{
    if( isalpha(userInput[i])  && isupper(userInput[i]) )
        count++;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

What problem(s) are you having? You can't take your car to a repairman and tell him "my car is broke please fix it".

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

replace the parameters with actual variable names

int main()
{
    int roll = 1;
    int answer = 2;
    int turn = 3;

    int x =  roll_hold(roll, answer, turn);
};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> //NOPE! Doesn't add 1st digit... because it's stuck in 'ch' b/c of inFile.get();

call inFile.unget() to put the digit back onto the stream.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Those are called function prototypes and they don't go in main(). Put them near the top of the program unit, after all includes. Alternatively, you can create your own header file and put them there.

#include <iostream>
#include <string>
// other includes here

int score_num(int& ns1,int& ns2,int& ns3, int& ns4, int& ns5, int& n6, char& gam_answ);
int roll_hold(int roll, int answer, int turn);
int turn_it_is(int& player1, int& player2);
void play_again(char letter);
void display_score(int curr_score);

int main()
{
   // blabla
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Why? Because the ISO standards committee said so. Didn't your momma ever tell you "because I said so" in response to your why question???

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

In c language you can't write a function that returns a pointer of some unknown type then typecast it to whatever you want. malloc() works like that because you tell malloc() how many bytes of data to allocate, and it gives you back a pointer to that block of memory. But the function you propose doesn't work like that. The parameters to your function are row and col, and the function doesn't know the critical piece of information -- size of each item. It has to know the data type (or size) of each element in the array.

you could write a function like this

void ** foo(int rows, int cols, int itemSize)
{
    char** ptr = malloc(rows * sizeof(char *));
    for(i = 0; i < rows; i++)
       ptr[i] = malloc(cols * itemSize);
    return ptr;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Please stop putting your questions in quote tags -- it just makes your posts confusing to read/understand.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Are you asking another question? Your post is confusing because you put a question in quote tags.

>>won't it be difficult to traverse a 2 dimensional array with a single pointe

You mean like treat double* ptr; as if it were double ** ptr; ? If you want row 1 column 2 then its just ptr[ (row-1) * ColsPerRow + col]; If course you will have to make sure the value os row is > 0 before making that subtraction.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Read this link.

In order for the compiler to produce meaningful references to line numbers in preprocessed source, the preprocessor inserts #line directives where necessary (for example, at the beginning and after the end of included text).

When the preprocessor parses your *.c program it will create a *.i file that expands all the #include and other preprocessor statements. In doing that all the include files are merged into one *.i file. When doing that it adds the #line directives so that it can keep track of the line numbers from the original file. Then when the compiler compiles the *.i file it will know the line numbers from the original files and use them to produce meaningful error messages. Without #line directives you would get line numbers from the *.i file, not the *.c or *.h file that you wrote.

I know of no reason why a programmer would want to insert #line directives in his program.

jephthah commented: thanks for parsing that. +7
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Don't do that even if you would. The macro is too large and will greatly increase the size of the final program. Just write a normal function that takes variable arguments.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Why would you want to make it void?

double** foo(int rows, int cols)
{
   double ** ptr = malloc(rows * sizeof(double *));
   <snip>
   return ptr;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

1. Because they said so. Why would you want to comment out the comments? If you want to comment out a large block of code that may contain comments, then there are other ways to do it. I do it like this.

#if 0
// code here
#endif

2. Not sure what you are asking. int array[200] = {1,2,3,4,5} In this array the first five elements are initialized to the values shown, all remaining array elements are initialized to 0. Is that your question?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Depends on the file's contents. As a general rule of thumb, just open the file as a normal text file and read its contents one line at a time. Beyond that, you will have to tell us what's in the file.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I thought since you need #include <ctype.h> for tolower() it is not standard. Why would you have to include #include <ctype.h> if it is a standard function?

All functions have to be declared before they can be used, whether they are standard C functions or not. Standard functions are not the same as C keywords as keywords are not functions, and there are no header files for them. There are lots of standard header files, and ctype.h is just one of them. Among the most used standard heder files are stdio.h, string.h and stdlib.h

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Write a C program to find the factorial of a number using do while loop?

No. You will have to write that yourself.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Read this tutorial. It has a section about menus and other resources.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

change the structure to use std::string instead of the char data. Then change the insert function to get a string from the keyboard instead of a single character. You will probably need to make other appropriate changes to your program as well.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

That is a pretty old compiler, so use '\0' if that works for you.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If I go to the US (33 years since my last visit!), who should I tip? I realise taxi drivers, waiters, porters, bellboys, busboys (or whatever they're called), consierges, coffee vendors, pizza delivery guys (only if their face looks less like a pizza than their product though) - but who else? Who decides? Is there a manual or a "bewildered's guide" for who you should and shouldn't tip?

If you offer a tip at the Wal-Mart store where I work I will have to refuse it. That has been done before.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Was this private insurance coverage or Medicare?

My heart operation was private insurance through my employer, but I now continue to be treated twice a year with the same doctor under Medicare and my military retirement health benefits. I just saw him a few weeks ago, had another stress test, MRI and two dimensional echocardigram. Its quite interesting -- they take a series of pictures of the heart then put them together to create a movie which shows the heart beating and the blood flowing. That's how the doctor can easily spot blockages.

My wife's operation will be combination of Medicare, and what that doesn't pay the remainder (except a small co-payment) will be paid by my military retirement insurance. No one knows what will happen under the Obamacare plan, but I suspect it will not be much different than what I get now. The new health care bill does NOT replace private insurance policies as it did in Canada, but just supplements it for those that have no insurance. We will be allowed to keep the same insurance we now have.

My experience may have been a lot different had I not had any insurance at all.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Is there really such as thing as time? According to this article time did not begin until the Big Bang. Before that, there was no space and no time. So, "what time is it?" is a meaningless question that is impossible to answer correctly.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

One way to do that is to create a structure that contains the strings and an integer

struct words
{
    char* word;
    int count;
};

Now make a simple array of these structures and initialize all counts to 0 and character arrays to the words in the list you posted.

Next, open and read the file one word at a time. fscanf("%s" ...) would be best in this program. For each word read, search the array for it and if found increment the counter.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The solution is pretty easy if you think about it. Every character in the English alphabet, and other characters too, have a numeric value between 0 and 255. To see what they are just google for "ascii chart". Given that, create an int array of 255 elements and initialize all of them to 0. Then read the file one character at a time, using the character as the index into the array. For example

int array[255] = {0};

array['A']++;

In the above the 65th element of the array will be incremented because the decimal value for the letter 'A' is 65. Instead of hardcoding the letter 'A' you would use the variable that you used to read the character in the file.

When you are done reading the file, all the consonants will already be in sorted order, so all you have to do is print them out. Loop through the array looking for array elements that are greater than 0 and are consonants.

Program done.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I like the second one, if it would compile :).

You're right -- should have been this: for (int k = first, m = 0; k < last; k++, m++)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You must then go into damage control mode and either say something extremely intelligent or witty in a follow-up post that makes everyone forgive you for your lousy earlier one or create some fake user who posts something completely unacceptable on the thread that takes everyone's attention off your real dumb post and draws it to your fake dumb post

Great Scott! I wish I had thought of that several years ago when I posted something really stupid. I'll remember that next time :)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

This isn't what you want:

printf("\"%s\"\n", buffer);
if(charin[strlen(charin)-1] == '\0')

charin[strlen(charin)-1] = 0;

j = strcmp(charin, buffer);

What you want is to get rid of the newline, not the end of string char, and you don't want to add a zero, either. The end of string marker char, IS a zero, but for char's - so your code is doing nothing but replacing one thing, with it's identical twin. ;)

What you want is this:

printf("\"%s\"\n", buffer);
if(charin[strlen(charin)-1] == '\n')  //if the string has a newline

charin[strlen(charin)-1] = '\0'; //replace it and shorten the string
// by one char. Now do the string comparison.                         
j = strcmp(charin, buffer);
//etc.

In C code, a group of letters without an end of string marker char, will compare the same as a proper string which has an end of string marker.

someLetters == someLetters\0

to strcmp().

Not so if one of the above has a newline char, however! :cool:

0 and '\0' are the exact same thing, so your solution is exactly the same as the one I posted earlier. When the compiler sees '\0' it replaces it with 0. You must be thinking it is '0', which is the character 0 not the decimal 0. In that case you would have been correct.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Also asked, and unanswered, here

urbangeek commented: hehe....nice find... :) +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I like the second example better, but delete line 14. Of course it won't work if you need to know the value of m after the loop terminates.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You don't call new to allocate COM pointers. Call CoCreateInstance()

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you have time (within 30 minutes of the original post) just remove all the text and write "<deleted>" or something like that, then PM one of the mods who MIGHT delete it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The Ctrl+] key (same unshifted } key ) can be used to find matching { and }, ( and ) or [ and ]. Is that what you are talking about?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

What time is it? When my customers ask me that, I usually sing It's Howdy Doody Time!

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

And for that $5000+ plus hospital and doctors' fees the Canadian has to wait half a year or more to get an MRI or CAT scan performed, 2-3 years for major surgery, etc. etc..

Compare that to what we receive here in USA today. I had heart surgery five years ago. Took a stress test then was immediately admitted to the hospital and was on the operating table within an hour. My wife is going to have her gallbladder removed, only two months from the time she visited the doctor. Need an MRI? I get them whenever I want, all I have to do is take the doctor's prescription to the MRI place and I get one right away, meaning wait in the waiting area for 10-15 minutes or so.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Depends on whether its of type float or double. Floats are "%f" but doubles are "%lf". I think long doubles would be "llf", which is a little like long long int (64-bit integers).

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>But it does not operate well.

What exactly does that mean?

lines 23-31: If the return value is -1 then the program prints an error message and continues to process as if the return value were ok ??? That's a little like when chopping an onion, you accidentally chop off a finger but say "That's ok, I'll just finish chopping the onion."

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Examine this program? Tell me its bugs

Well, all you have to do is run it several times and you will find out the bugs.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Public socialized health care doesn't mean free health care. Here is a good article, I think, about Canadian health care system.

The first thing to realize is that free public medicine isn't really free. What the consumer doesn't pay, the taxpayer does, and with a vengeance. Public health expenditures in Quebec amount to 29 per cent of the provincial government budget. One-fifth of the revenues come from a wage tax of 3.22 per cent charged to employers and the rest comes from general taxes at the provincial and federal levels. It costs $1,200 per year in taxes for each Quebec citizen to have access to the public health system. This means that the average two-child family pays close to $5,000 per year in public health insurance. This is much more expensive than the most comprehensive private health insurance plan.

<snip>
At zero price, no health services would be supplied, except by the government or with subsidies. Indeed, the purpose of a public health system is to relieve this artificial shortage by supplying the missing quantities. The question is whether a public health system can do it efficiently.

As demand rises and expensive technology is introduced, health costs soar. But with taxes already at a breaking point, government has little recourse but to try to hold down costs. In Quebec, hospitals have been facing budget cuts both in operating expenses and in capital expenditures. Hospital equipment is often outdated, and the number of general hospital beds dropped by …

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If the line read by fgets() is a blank line I suppose you could just ignore it and read the next line.