deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Why is it safe to use delete[] without getting a crash or something?

You consider undefined behavior "safe"?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

for (i = 0; i < gsize_ptr; i++){

gsize_ptr is a pointer, you need to dereference it:

for (i = 0; i < *gsize_ptr; i++){

Of course, looking at that function definition I see no reason why gsize_ptr needs to be a pointer in the first place, unless that's not the final definition and you plan to ultimately assign a value to sizeOfGames from descendingOrderByTraining().

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It's totally easy. Take a look:

#include <stdio.h>

int main(void)
{
    for (int i = 1; i <= 5; ++i) {
        for (int j = 1, x = 2 * i; j < x; ++j)
            printf("%.*s%d%s", j>1?0:2*(5-i),j>1?"":"\
                   ",j<=i?j:i-j%i,j<x-1?" ":"\n");
    }
}

Mwahahahahaha!

But seriously, you need a loop for the rows, a loop inside to handle the increment part, and another loop immediately after to handle the decrement part. I wouldn't recommend handling both the increment and decrement in a single loop (like above) as it complicates the algorithm unnecessarily.

You also don't need to make a pyramid yet, but I suspect that's going to be the next thing your teacher asks for. To handle that you need to print a decreasing number of spaces before printing out the numbers for each row. The spaces will start at half the length of a row and end at 0.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

if you don't want to help me. it's fine.

Okay, I'll even close this thread because it's in violation of Daniweb's rules.

thanks for calling me that without even knowing me and how hard i tried.

I call it like I see it.

If you want people to know how hard you tried, you have to prove it. We simply won't take your word for it, thanks to all of the cheaters that come to this forum looking for a handout.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I think this post should be submitted for the laziest leech student of the year award. How do you sleep at night?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

And why would you rely on a simple C function in a managed language?

Because this thread was originally posted in the C forum and gerard probably didn't notice that the code was C#.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Chances are pretty good that you won't be able to stop it from being delivered, but out of morbid curiosity, what delivery method are you using? Email, I assume?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It is wr,tten enough to solve it besides

No, it's not.

besides, if you dont want to reply you dont so it is not neccessary for that replying ;-)

Then you don't mind if I just close this thread as being in violation of Daniweb's homework rule? Okay, I'll go ahead and do that. Thanks for playing.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Thanks for enlightening me about MIPS, Schol-R-LEA. But I think you totally missed the point of my example code which was meant to highlight the concept, not provide a real solution to the problem for the target architecture. The assumptions I made are largely (if not totally) irrelevant, unless you can also confirm that C compilers targeting a MIPS system will use a calling convention that's completely different from and incompatible with the one I described.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

actually, I'm not able to understand this topic!!

Then read a book or ask a specific question.

Is your code correct? Yes, it compiles and runs nicely. If it doesn't do what you want because you don't know what you want or have any clue how the formatting modifiers work, the problem is obvious.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What output were you expecting?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I also don't really miss them all that much, to be honest.

For the usual suspects it's not a big deal because most people recognize them. For things like :rolleyes:, I miss them, and I can think of others that I could make use of like :sarcasm: and :notsureifsrs:.

Since you're scared, I'll see about taking a little time to evaluate how much effort would be involved. I was thinking that there was another reason why you hadn't jumped on it yet.

;D

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yummy, I like it. There's a good selection of forums I'm likely to look in a session. While it's a minor thing, being able to mark the forums as read from there with a single click is nice (it's minor from the user perspective, of course. I'm still traumatized by the mark as read algorithm).

On an unrelated side note, what's the 411 on graphic smileys?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Your question seems sketchy. However, I'll give you a chance to elaborate rather than simply deleting it as illegal activity.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Thats just how my instructor wants it.

You're misunderstanding the question. Your teacher wants you to use pointer arithmetic when accessing the "array" rather than subscripting with an index. For example:

smallest = *(f + i);

Is there any problem with my program

Well, your algorithm is completely wrong for what it's trying to do. I suspect you just jumped in and started writing code without working out all of the potential case examples in a small set on paper. If you don't understand the problem or how to solve it methodically, you can't write a program to do it for you. Here are the cases I'd use. First, the average case permutations:

  1. 1 2 3
  2. 1 3 2
  3. 2 1 3
  4. 2 3 1
  5. 3 1 2
  6. 3 2 1

Then obvious degenerate cases (such as an empty array or no second smallest number):

  1. <empty array>
  2. 1
  3. 1 1
  4. 1 1 1

Once you can lay out detailed steps to accomplish the goal in each of those cases on paper, you can start writing code to do it.

or is it just pointer in 'C' that have some problem??

Pointers in C have no problem, you're just using them incorrectly. Don't blame the language for your poor use of it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I dont know how to use pointers.

That's your problem, and the fix is learn how to use pointers. But since you'll probably get mad if I don't offer an exact solution to your immediate problem, change this:

t_matriu *Crea_M ( int dim )

To this:

t_matriu Crea_M ( int dim )
Despairy commented: hahah... good one +2
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It has nothing to do with the "array" and everything to do with the wacky use of pointers for smallest and secondSmall. Just make those regular float variables, there's no need for pointers there.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Given that the code was pinched verbatim from this article, my initial question is why do you think it needs debugging in the first place? Does it not work? That's somewhat likely, but I'd like to believe the article's author wouldn't publish untested code. Or does it just do something different than you wanted?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

1, all the times I have done a comparasin, > < == in the past there has been an else, in this their is none

It's implicit. You can pretend it looks like this if you'd like:

if (a+b > c+d)
    return func(a + b, c + d);
else
    return func(a + b, c + d);

and secondly how does one return func and set the paramaters like in the C.

Typically the caller handles parameters by pushing them onto the stack (in reverse order for the C calling convention) before making the call, and cleans up the stack after the function returns:

; Add the parameters to the stack
push d
push c
push b
push a

; Call the function
call f

; Remove the parameters from the stack
; I know nothing about MIPS, so assuming int is 2 bytes
add sp, 8

So as the function author you don't need to care how it happens as long as it happens with the expected result.

The return value varies, but the most common scheme is a hybrid of using a preselected register (the accumulator register in x86, for example) for smaller values and a hidden parameter for larger values. In your function, a return type of int is simple enough to place in a register, so there's no wasted effort working with the stack. But the hidden parameter would something look like this:

; Add the parameters to the stack
push …
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

i'm using ssh to access my school's venus account, and am doing my work directly within ssh. is that my problem?

I have no idea how to interpret that into an answer to my question. Regardless, the code is correct; of that I'm quite sure. If your school's compiler doesn't accept it then the compiler is either wrong (unlikely unless it's a pre-standard compiler), or there's some kind of issue in passing the text to the compiler for processing (more likely). Maybe there's an issue with newline conversions in the source code, or control characters, or whatnot.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

is because i'm not familiar with the common (more advanced methods) yet.

The method I suggested is simpler and easier for a beginner to understand and/or figure out.

i put your code word for word into the compiler, even down to putting whitespace where you do. thoughts?

What compiler is it?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You could certainly do it that way, but it's excessively complicated. The pattern adjusts predictably for each row, so just keep it simple:

#include <iomanip>
#include <iostream>

using namespace std;

int main(void)
{
    int distance = 7;
    int i = -1;

    while (++i < distance / 2)
        cout << setw(i) << "" << '+' << setw(distance - i * 2 - 2) << "" << 'X' << endl;

    cout << setw(i) << "" << '*' << endl;

    while (--i >= 0)
        cout << setw(i) << "" << '+' << setw(distance - i * 2 - 2) << "" << 'X' << endl;
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

in simple words tell me PI is an valid variable name or not???????????

Technically, yes. In real life, it depends on your compiler and the libraries you're using. If you need simpler words than that, the answer is no.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Is it generally a better practice to define all the functions you're going to use before you define main() ?

It depends on your prefered style, neither is better than the other. However, for larger projects you'll end up moving the declarations to a header anyway, and the definitions to an implementation file. So the choice of defining before or after main() only applies when you're not modularizing those functions.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What does this error mean?

It means you must declare something before you use it. Specifically, getMatrix() and displayMatrix() are defined (and declared) after main(), but you use them in main(). Add a prototype to declare each:

void getMatrix(int number);
void displayMatrix(int number);

int main(void)
{

More fundamentally, is my code inherently bad? As in, is it a poor way to achieve what I wanted to achieve?

Inherently? No, but it could be improved. However, those are nitpicks that are best left for later.

Why is coding so hard? :(

Programming is difficult. Welcome to reality.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Am I on the right track at least?

Yes.

I still have no idea how to actually print a month.

Here's an example of what I was talking about:

#include <iomanip>
#include <iostream>

using namespace std;

int main(void)
{
    const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int wday = 3;

    // Print this year's calendar
    for (int month = 0; month < 12; ++month) {
        // Print leading blanks for last month
        for (int i = 0; i < wday; ++i)
            cout << setw(3) << "";

        // Print this month's calendar
        for (int mday = 0; mday < mdays[month]; ++mday, ++wday) {
            if (wday == 7) {
                cout << '\n';
                wday = 0;
            }

            cout << setw(3) << mday + 1;
        }

        // Set up for the next month
        if (wday != 7) {
            // Avoid one too many blank lines if the 
            // month ends a linebreaking weekday
            cout << '\n';
        }

        cout << endl;
    }
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

math.h might define M_PI but this is only if
__STRICT_ANSI__
is defined.

Everything in this statement is making an assumption about the implementation. Neither M_PI nor __STRICT_ANSI__ are required by the C standard. M_PI is a common extension though, so it would be best to safely define it if you use it in portable code:

#ifndef M_PI
#define M_PI 3.1415926535
#endif

And because both PI and M_PI have well established meanings, it would be unwise to use those names as anything but a constant for the value of pi. Or at most a working variable that eventually ends up at an approximation of pi.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Well, you know how many days are in each month, and what weekday the year starts, so the algorithm stands out immediately as a couple of nested loops with a few counters that wrap around at appropriate times. You're looking at around 10 lines of code.

Don't over complicate things, this is almost a no brainer. Of course handling leap years and error handling the weekday input are both minor complications, but it's not a big deal. See if you can come up with an algorithm on your own.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

assert( ! inStream.fail() ); // make sure file open was OK

And if the file wasn't opened okay you'll hard stop the process with a cryptic error? Sounds like the pinnacle of user-friendliness. :rolleyes: assert() is intended to help the programmer debug impossible errors at development time; it's not meant to handle likely runtime errors that are out of the programmer's control (ie. user input, file existence, etc...).

I'm stuck on the second part of my program. Everytime I run this code I get "ConsoleApplication4.exe has stopped working".

Your process hard stopped with a cryptic error... It's a curse being right all of the time. ;) I have a better idea, check for a successfully open file with an if statement and print a useful diagnostic if it fails:

inStream.open( "dictionary.txt");

if (!inStream) {
    perror("Error opening dictionary.txt");
    return 0;
}

perror() will likely give you a more useful error message as to why the file couldn't be opened, but you'll need to include <cstdio> for its declaration.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'd hold on to it. You never know when you might be asked to work on a legacy project that requires that version of Visual Studio.

In fact, I have a copy of Visual Studio 6 both at home and at work precisely for that reason. I've benefitted greatly from it at work because some of our older projects have come back to bite us (one instance happened not a month ago with a VB6 project), and having the software to work with them was invaluable.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

This is my original code

That's also the third time you posted your original code; we get it. If you're not willing to read our replies and do the work suggested, while instead asking for "help" with the conversion that clearly means "do it for me", kindly piss off.

I think I'm done with this thread, since it's becoming more and more apparent that you're a leech. kthxbye.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Can u help me to convert ???

Like I said, you can't do it with floating point. If you want to do this with a switch, which would be stupid in my opinion, you really have little choice but to handle all marks as integers. Even then you'd have something insane like this:

switch ((int)round(marks)) {
    case 0: case 1: case 2: case 3: case 4: case 5:
    case 6: case 7: case 8: case 9: case 10: case 11:
    // ...
    case 34: case 35: case 36: case 37: case 38: case 39:
        ++student->getF;
        student->marks[n] = marks;
        *sumOfData += marks;

    // Repeat for the other marks
}

It's hideously verbose and very easy to make mistakes.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Let's start with the two most obvious bugs:

  1. You compare two values with the == operator, not the = operator. The = operator is for assignment.
  2. if statements and loops with more than one statement in the body must be wrapped in parentheses. It'd be a good idea at first to always use parens to avoid subtle bugs. C is not Python, indentation means absolutely nothing to the compiler.
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The switch statement isn't well suited to ranges, and it also doesn't support floating point cases, so you're SOL in converting the grade calculating from an if chain to a switch.

May I ask why you need to do this? Is it a requirement from your teacher?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Either get a new compiler (free) or forget about your program.

You could queue up the keystrokes and process them in sequence. Provided they're processed quickly enough, the end result should be comparable to a threaded solution. It's not like games weren't written with Turbo C when it wasn't too old. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

any links to some good C11 resources ?

For library details concerning all standards, this is available. Honestly, I just refer to the draft standard, but that takes practice due to the legalese it's written with. I'm not aware of any books or decent tutorial websites available yet.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

i think all this bad hype about strtok() is only coz people who dont know about these issues with it, put it in their code n think everythings gonna work fine, and then, they hit run ....... and crash! :P

Bingo! There's really only one true case of a function being straight up "bad" across the board, and that function is gets(). The reason it's bad is because there's literally no way to make a call to gets() safe. Even atoi(), my next choice for the canonical "bad" function, can be made safe with careful validation around the call.

Fortunately, gets() was removed in the latest revision of the C standard.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

character=getch means?????

It means you need to realize that this isn't Twitter. You don't have a 140 character limit, so please engage your brain and try to communicate like an intelligent human being. You'll get much better answers if you ask comprehensible questions.

kthxbye.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

on another note, what is strtok() good/designed for?

Tokenizing strings. But it's not designed to be reentrant, or designed to work with read-only strings, which is pretty much all the article you linked to complains about. I was kind of hoping for an actual argument against strtok() rather than the usual rehashed "it doesn't do what I think it should". :rolleyes:

so thats how strok() should only be used?

Obviously. I mean, if you know it's not okay to modify the string or you can't make a copy then clearly strtok() is the wrong choice. I'm not saying that you should always use strtok(), but anyone who says strtok() is evil and should never be used is an idiot.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

But still, I'm curious, why does (*this)[i] work?

Because you're dereferencing the this pointer and then accessing the overloaded [] operator (which is technically a member function). While it may look the same as a[i] in your example, under the hood the two are very different.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Here's one possible example of using bitwise operators for this:

int divide(int dividend, int divisor)
{
    unsigned long lhs = dividend;
    unsigned long rhs = divisor;
    int result = 0;

    if (rhs == 1)
        return lhs;

    while (lhs > rhs) {
        unsigned long quotient = 1;
        unsigned long sub = rhs;

        while ((sub << 1) <= lhs) {
            quotient <<= 1;
            sub <<= 1;
        }

        result += quotient;
        lhs -= sub;
    }

    return result + (rhs == lhs);
}

I'll leave optimization and handling of negative numbers to you.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

im reading this page about strtok() being bad... and i quite like it... has quite a detailed explanation of things..

"Bad" as in "not designed the way I would have designed it". It certainly points out the issues that you need to keep in mind when using strtok(), but a program breaking when you use a function in a way that actively and intentionally goes against its design doesn't make the function bad, it makes you a bad programmer.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Post all of your code. If it's too long, shrink the code down into something smaller, but still complete and compilable.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'll post something substantial when I have a bit more time, assuming someone else doesn't beat me to the punch.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You didn't check to see when A equals 120. The implicit check is against 0, so your loop actually looks like this:

while (A++ != 0) {
    B += 10;
}

There are a number of things that can go wrong here, which you can limit a bit by changing the test to this:

while (A++ < 120) {
    B += 10;
}

However, it would also be wise to limit the lower end too, just to ensure that you're not starting with A at something like INT_MIN. If that happens then B will easily overflow:

if (A < 0) {
    /* Assuming negatives are an error, for the sake of the example */
}
else {
    while (A++ < 120) {
        B += 10;
    }
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Try this instead:

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    ifstream in("output/alignskewresultsgenesnormal.txt");

    if (in) {
        string line;
        int n = 0;

        while (getline(in, line)) {
            cout << line << '\n';
            ++n;
        }

        cout << n << " lines detected" << endl;
    }
    else {
        cout << "Error opening file" << endl;
    }
}

I want to rule out issues with the broken use of eof() as a loop condition and make sure that errors are properly checked.

p.s. Your code is C++. Should I move this thread to the C++ forum, or are you really trying to write C?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Then read from the file and when you run into array boundaries expand it using realloc().

An alternative would be a two pass algorthm that counts the number of lines and maximum number of columns for the first pass, then allocates the array and populates it in the second pass. That way you avoid expensive calls to realloc(). Whether this is more efficient would need to be tested though, as reading from a file can be expensive as well. ;)

I think the two pass solution is cleaner and would be easier to understand for a beginner.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

so i think it is not that much bad question like you thinking it ;)

Nope, it's still a bad question. Unfortunately, employers and the like have this annoying tendency to pick up stupid trivia and techniques that haven't been productive since the 50s. They're hardly viable as measures of one's knowledge or skills, but it is what it is.

I agree with Gonbe though, the expected solution is highly likely to involve bit twiddling.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

that sounds good... any code exapmples?

That's something you could (and should!) search for yourself, but I'll bite:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char src[] = "This is a test"; /* Note that it's an array and not a pointer */
    char *tok = strtok(src, " ");

    while (tok) {
        puts(tok);
        tok = strtok(NULL, " ");
    }

    return 0;
}

The single most important thing to remember about strtok() is that it modifies the source string. Thus you can't use a pointer to a string literal, and if you want to retain the original string you must make a copy for strtok() to consume.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I can't decide which answer is most appropriate, so I'll provide both:

  1. Do your own damn homework!
  2. Please provide proof of effort and then ask a specific question if you want help with this problem.