WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

And format your code. It's too hard to follow with inconsistent spacing.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Please format your code. We need to be able to follow it in order to be able to help.

And reading this will help you give us the info we need.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

The main thing I'm not sure of is how to step through the list like I would with java.

Stepping through the list is done by calling getFirst() which, I assume, returns the first node in the list. Then passing that node into getRest() returns the next node. Keep calling getRest() to step through the rest of the list.

To insert a node, find the position (the node that should follow your new node) always remembering the node you just left. Then readjust the pointers to insert your new node.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Might I suggest an "annoying ad" button, that we can click if the ad annoys us to the point where we would not touch the product? The advertisers might like the knowledge of the diseffectiveness of their ads. If they knew how annoying their ads really are, they might change their ways.

Boy is that an interesting suggestion. I can see someone clicking every button simply because they don't like ads.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

//edit: sorry for the double post.

If you don't want want to double post, you can edit your first post. But double posting is not a problem if you have additional information. It's bumping we frown on.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

When you post a request for help, you need to tell us what's wrong. Don't just tell us there's an error and expect us to look for the error.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

The statement "they r just not working" tells us nothing. You need to tell us what the code is doing, and what it is supposed to be doing instead, as the post titled Read Me: Read This Before Posting suggests.

It's obvious the code you posted does not compile, it's undoubtedly psuedo-code. Is there something wrong with that algorithm?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

There is so much wrong here!!!

Both of you, please read this about gets().

This loop (reformatted for readability):

#
for (n = 0;  n < 11; n++)
{
    if (strncmp(one[n],"xxxx xxxx",10) != 0 && 
        strncmp(two[n],"x,xx xxx.",10) != 0)
    {
            cout<<"Mismatch";
    }
}

has the following problems:

1) the variables only contain 10 character locations and the loop goes through 11. The last value is therefore outside the array boundries. Only go to n < 10 2) one[n] is a character, not a pointer to a character array. strncmp() only works on pointers to array.

3) Even if the compare worked, the second time through the loop you'd start at one[1] and compare 10 characters. The last character compared is again beyond the bounds of the array. Next loop is one[2] so 2 characters are beyond the array. And so on.

Another couple more things you should probably read are the Rules and this post titled Read Me: Read This Before Posting

hammerhead commented: ty for gets +2
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Use the toupper() function from ctype.h:

else if (toupper(userPlay) == 'P' && toupper(cPlay) == 'R')

Also, you should only call srand() once, so move it to the beginning of main()

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Could it be there are no input statements in your code?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Also, please read this series on scanf() , especially the character and string entries.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

It removes up to INT_MAX characters from the input buffer, basically clearing the buffer.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

People,

Let me clarify my query.
I'm not looking for code for this problem. I said 'Write a C function/algorithm to do the same' in my original post, because I was quoting the exact problem statement (as asked by the interviewer).
I pretty well know the logic (and code) for transposing a rectangular matrix in-memory. The only thing I need help for is how to take care of overlapping cells in this copy-paste operation. The transpose program is quite straight-forward but we've to ensure that no data-loss happens while doing this. So, any tips regarding that are welcome. No need to give any code.

thanks!

Absolutely none of this information was apparent in your first post. Be sure you give all necessary information the first time.

1) Use 2 arrays
2) use an n+m x n+m array

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

And you should have read The Rules. PERIOD.

Well, comma, actually.

Are you asking us to supply a program for you or help you write something?
Would an Excel Spreadsheet be enough, or do you need a program?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

>> No it doesn't. It is legal to have nothing in the TRUE position of an if
yes, it does. having nothing is not legal C++. however, the statement may be
a null statement ( just a ; ) or an empty compound statement ( {} ).

I consider just a ; to be a nothing, but you're right. I did gloss over that fact you need it. Sorry 'bout that. Thanks, Vijayan

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Did you fix numbers?

Since you changed your code, you might want to share it with us.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

John is correct on both counts. Change that nested IF into a switch and it'll be easier to read.

I see a while statement in case 1: -- what is that supposed to be doing?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

please help me out how to solve this poblem...

Please help me out how to solve this problem....

Can anyone help me out...

Please help me out how we can solve this problem...

Jeez! Stop begging! Aren't we trying to help you? :icon_rolleyes:

[edit]The particular thing that caught my eye was a directory listing.
To me this says that you may keep reading non-existent data.

[aside]This kinda thing is pretty much in the ballpark of why I dislike the "read to eof" idiom of input gathering.[/aside]

Dave is referring to the line while(!myfile1.eof()) in your code. See this for an explanation why it's a bad thing to use -- feof() and .eof() are identical.

And after you fix the .eof() , maybe it's time to post your current code. I assume you've made changes.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Guys, guys, we're supposed to be 'experts' here! :D

#1: You are using a for loop but your exit criteria of the loop depends on counter and limit. Your instructions say "when the user enters a negative number or zero" and counter has no bearing on the exit.

#2: After you input your value, whether positive or negative, you process the value.

Recommendation:
#1: Try a different type of loop. There is a better choice, although for will work, it's just a little clumsy.
#2: Is there a better place to input the value within the loop so it doesn't get processed?

Dave Sinkula commented: Good clarification. I'm trending terse lately. +13
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

also, look up >> and see what it means.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

No, what he said is the headers have no code associated with them. Headers don't do anything. It's the libraries (code) that are associated with the headers that process the functions. Without them, the header is useless.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

That's because when there is only one line, brackets are implied and therefore not needed. (Although I would recommend them, especially when starting out.)

Completely agree! Always use brackets within an if statement

Welcome to Daniweb!

Yeah, what he said! :)

The error message probably means that you should have a statement of some sort between each if and each else.

No it doesn't. It is legal to have nothing in the TRUE position of an if .

Follow cscgal's advice and you will automatically solve your problem. And you will probably figure out what it was, too.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

And WaltP, I will remeber to code with mega amounts of whitespace!!!

Please don't! Use appropriate amounts. The following is just as bad as none :icon_wink:

if   ( a  ==      b)
{
                      j    =      alpha      *      89;
                      cout        <<        "Answer #1"           <<
                                               j   <<            endl;
             }

:icon_twisted:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

What values are the variables going to have if there were no numbers in the given range? Test for those values before you output the min/max.

Hint1: look at the special value you used.
Hint2: what does testing the last value entered (x) when you output your results do for you?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

First of all, don't use ICODE tags unless you want to show code inline with your message.

Your code is not too bad, but here are some comments for you to think about:

if (input[b] >= 85)
      {
        hcnt = hcnt +1;      // you can also use hcnt++;
        inp = input[b];      // why waste another variable?  
                             //    You can use input[b] anywhere

        otl = (sum + inp);   // what are these two lines doing?
        sum = otl;           // can't you just say sum = sum + input[b]
                             //    or  sum += input[b]?

        ave = sum/7;         // you don't want to compute the average 
                             //    while inside the loop
        
      }
    else if ((input[b] >= 65) && (input[b] < 85))
      {
        wcnt = wcnt + 1;      // same comments
        inp = input[b];      
        otl = (sum + inp);
        sum = otl;
        ave = sum/7;
      }
    else if (input[b] < 70)   // so all temps from 65 and 69 are 
                              //    both warm and cold?
      {
       ccnt = ccnt +1;
       inp = wcnt + 1;        // why is inp number of warm days+1?
       otl = (sum + inp);
       sum = otl;
       ave= sum/7;
      }

////  calculate your average here.

        printf("The average temperature is %d\n\n", ave);
        printf("The total of Hot days is %d\n", hcnt);
        printf("The total of warm days is %d\n", wcnt);
       printf("The total of cold days is %d\n", ccnt);
   return 0;
}
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

We don't give code as answers to questions. We help you with the code you write. So try it and reply with your attempt.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Well, it's not a placeholder. You are defining arrays with no rows and no columns. There is no place for data. So once you try to put anything in your arrays, you overwrite memory changing data you shouldn't and will definitely screw up the program and probably crash.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Since I've never heard of Bingo/Chess and have no idea what that means, there's not much I can do from a logic standpoint.

And since there is absolutely no formatting in your code, there is no way I can understand what you have written.

Post enough information about your problem as recommended in Read Me: Read This Before Posting and format your code properly, then more people might be able to help.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Problem 1, (lines 37 -43). The program will correctly count the number of integers in the .txt file and will create the correct size array. But it will not list the actual numbers from the file. For example, input file "num8.txt" has 8 integers from 1-8. So an array of length 8 will have 8 integers in various order. How would I get each integer to sort it? I thought of using the following code...

In the while loop you need to load the value just read into the array. What you are doing is reading all the values in the while, then loading the last value read into the array with the for loop.

Problem 2: When compiled the output will all be sorted correctly on any file with 64 integers or less. The program crashes with files that have larger numbers of integers. I can not understand why I would run out of memory as I have declared all int values as longs.

The size of your variables have nothing to do with the number of values in an array.

You've declared

int main()
{
  long count = 0; //counts the number of numbers in the input file
  long numbers[count];

You just declared numbers to be an array of 0 values. Are you getting a warning or error on the long numbers[count]; statement?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

(I'll accept any reasonable solution to handling the first two numbers)

So nice of you :icon_wink:

This isn't working for me, and I'm not sure how to fix it or how to get the average of only the last three inputted numbers.

Is there something about explaining why you think it doesn't work that is just lost on new posters? If you were going to help someone, wouldn't you want to know what's wrong? :icon_rolleyes:

There's an obvious error in this code. Let's assume x=3:

num1 = x;        // num1 now = 3
       num2 = num1 + x;   // num2 now = 6 (3+3)
       num3 = num1 + num2 + x;    // num3 now = 12 (3+6+3)
   
       cout << num3/3 << endl;

How is this averaging anything? Think through how to average 3 numbers again. Maybe do it on paper to get the process figured out step by step. Then translate those steps into code.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Ad campaigns cannot simply be removed. Each campaign is often the result of weeks of negotiations resulting in signed contracts.

Since that is so, maybe a requirement that the add is non-intrusive be added to each contract so you can better control the working of your site. And while you're at it, no flashing ads, so you can control the look of your site. I know I'm not the only one that hates flashers.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Was it in the C/C++ forum? If not, I did not look at it. I have absolutely no reason to even look at the Pay-Per-Click Advertising forum. Nor any Site Management forum.

So yes, 1000+% sure. :icon_wink:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
#include "stub.h"
int main()
{
   int x = 0;
   int highest_pos = 0;
   int lowest_pos;
   int counter;
   int highest_neg;
   int lowest_neg;

The values lowest_pos, counter, highest_neg, and lowest_neg start with unknown values. They could be 0, or 165432343, or -8554545487. You need to initialize them to some appropriate value.

Keep in mind if you initialize lowest_pos to 0 what will happen when you execute

if ( x < lowest_pos && x > 0)
    lowest_pos = x;
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I repeat:

First, think about what the program is supposed to do. What has to happen for one equation? If there's a 'get another' something, there's a loop around that something. If there is a 'calculate another' something, there's a loop around that too. BUT -- do you need one loop, or two?

Use pen and paper and write an equation to calculate. Below that, write down the variables you've defined in your program. Now, verrrry slooooowly do the calculation, one small step at a time filling in values for each variable as you go. Think in terms of the ifs, the switch, and the loop. What operations do you do more than once? In what order?

So do an equation completely by hand, step by step, and write down each step. Then look at those steps carefully. Then write another and solve if by only following your steps -- this time without thinking. If you get the correct answer, convert your steps into code.

And thanks, niek_e. Maybe I should have just written it for him, too. Save you the trouble :icon_confused:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I am so confused now! I cleaned up the formatting, fixed the indenting, rid of some of the 'if' statements , is it better?

Much!! A couple problems but much better.

The program will not calculate at all so I really did something wrong. I am a bit thicke and this the 13th straight hour I have been working on this is getting to me.

Can you in great detail (so I can understand) tell me what direction I should go?

You ain't gonna like it. Here's the direction you need to go -- think! See I told you :icon_wink:

OK, so you moved your loop above the switch. When is the switch useful? After you've input all your numbers and operators? And since you have no way to exit the loop, how do you expect to get to your switch?

First, think about what the program is supposed to do. What has to happen for one equation? If there's a 'get another' something, there's a loop around that something. If there is a 'calculate another' something, there's a loop around that too. BUT -- do you need one loop, or two?

Use pen and paper and write an equation to calculate. Below that, write down the variables you've defined in your program. Now, verrrry slooooowly do the calculation, one small step at a time filling in values for each variable as you go. Think in terms of the ifs, the switch, and the loop. What …

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Ok, here is what I have now. I am still getting errors when it comes to calling the function and passing the values.

Such as? :icon_rolleyes:

Psychically debugging code is hit or miss proposition. Mostly miss. Unless you give us information, there's not much we can do...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Then you need one if for each guess. They don't have to be nested, either. Each if simply tests the guess with the number and if they aren't the same, do what you need to do.

Within each if, get another number, test it for > and < and output the appropriate message. No need to even consider equal until all the ifs are finished.

As you can see, it helps to give us all the information so we don't waste your time giving you advice you can't use.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

First thing -- please use better formatting. You have too much indentation and not enough whitespace in your code. Look at your post above. Notice how it's hard to follow?

If you are going to use a switch() statement, why are you using all those if statements? Use one or the other, no need to use both.

You are using while(1) for your loop. When will it exit? Put a condition that meets your exit criteria and you won't have to test for it using an if in the loop.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Also, here is a piece of logical truth. Given

if (a < 10)
    do this...
else if (a > 10)
    do this...
else
    when you reach here, isn't a == 10 automatically? No need to test it.
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

thank you.

just a few more questions

My professor taught us to use getch to see the output.
I'm not familiar with using it as a replacement for scan.

So? My mom taught me fire was hot. Then after research I found it can cook food. Do some research

So here is the question.
getch() just gets 1 character right?
How could i use it to get a string?

would it be like example:
---------------------------
getch(1st letter)
getch(2nd letter)
--------------------------
And just loop it?

Yeah, kinda.

And last question. I'm a little confused with kbhit
kbhit() returns true only when a key is pressed

So the program would continue only if the user presses the correct key?

Sorry for a lot of questions. I haven't used kbhit before

So? I repeat -- research. Look it up. Google or programming manual for your compiler.

It's going to take you a very long time to program if you have to ask a forum how something works and wait 12-48 hours for an answer. We can best help you clean up a problem or steer you in the right direction, but to answer questions like these is a waste of your time. Look up what you need. Then ask what you don't understand.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You need to look more closely at where your loop is. Your code:

input a number
test your operator (it was never entered)
    process the operator
start a loop
    input operator
    if operator is illegal 
            output error
    else  input number
end loop
...

You never test any operator and process the result except the one you didn't enter.
Once you start the loop you
1) never exit the loop
2) never process an operator or the numbers

Think about where your loop should really start, and what should cause it to exit.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

With todays compilers, you don't. Those headers are no longer used.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Yes, you need a loop and a counter to count how many time your loop executes. Exit when counter == 5 OR guess == random_number

Also be careful using = and ==
They are not the same and you have to use the right one for the right purpose.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

By not using scanf() to input your data. See this series to see why, and how to fix it.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

On subscription spy I saw:

Pay Per Click ??
in Pay-Per-Click Advertising with 13 replies and 4,097 views
You have no posts in this thread
Thread viewed
by WaltP

I was replying to a thread in the C forum, and never opened a thread called Pay Per Click ??

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

See these links:
#1) void main()
#2) scanf("%c"...);
#3) Code Tags
#4) Works for me

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Why don't you click the link I gave you?

Because it must be easier to waste time and energy asking questions than actually doing research.

After all, we know it all, don't we? :icon_wink:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

P.S Refer to my reply to your mail.

As I'd said privately....

No one should be answering private requests for help. That's because no one should be asking for private help. Help is what these forums are for. Keep all questions and answers in the forums, please.

Not disputing what you say, Dave.

It's a matter of what the OP's problem is here, and how I (and at least one other) have been trying to help him around it.

And Dave is trying to clear up misinformation being passed to the OP. Though not directly related to his problem, telling him there's an EOF character is teaching him 'bad grammar' which he will pass on to the next person. So don't argue with Dave. He's trying to help, too.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

got impatient ant tried it on a compiler. seems to work alright.

Gee, maybe you should try that first next time... :icon_rolleyes:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

He left his email right there on the page you linked to. Try that.