deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Please review the following code and compare with yours. Take note of the comments and try to understand why I made the changes that I made. I tried to keep the spirit of the algorithm the same, even though I'd probably do this differently:

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

int main(void)
{
    char s[] = "I Am Sad";
    char str[10] = ""; /* Make sure to initialize str because its length is taken shortly */
    int i = 0;
    char *st2;
    char *st;

    st = &s[0];

    while (*st != '\0') {
        if (*st == ' ')
            st2 = st;

        *st++;
    }

    for (; strlen(str) < strlen(s);) {
        /* What if st2 == s? */
        if (*st2 == ' ')
            ++st2;

        st = st2;

        /* Don't reset i if you want to append to str */
        while (*st != '\0' && *st != ' ')
            str[i++] = *st++;

        str[i++] = ' ';

        /* Don't forget to skip whitespace or you'll always process the same word */
        while (st2 != s && *--st2 == ' ')
            ;

        /* Now find the previous word. Don't forget to check for the start of the string */
        while (st2 != s && *st2 != ' ')
            --st2;
    }

    str[i] = '\0';
    puts(str);

    return 0;
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

inheritance (n):

  1. something that is or may be inherited; property passing at the owner's death to the heir or those entitled to succeed; legacy.

  2. the genetic characters transmitted from parent to offspring, taken collectively.

  3. something, as a quality, characteristic, or other immaterial possession, received from progenitors or predecessors as if by succession: an inheritance of family pride.

  4. the act or fact of inheriting by succession, as if by succession, or genetically: to receive property by inheritance.

  5. portion; birthright; heritage: Absolute rule was considered the inheritance of kings.

Ask a real question next time, thank you.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

deptName is a private data member from studentType header

Then you can only use it from within the studentType definition.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Why are we allowed to delete memberships now?

Because we got tired of long whine threads about how we were satan's spawn because account deletion wasn't supported. The new system made it easy enough to add, even though the reality is that it's a feel good feature that accomplishes a whole lot of nothing other than shutting up a vocal minority.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Or am I just clutching at straws? Haha

You make valid points, but those points are akin to saying that it's simple enough to kick a door in so you may as well leave it unlocked. The harder you make it to break the system, the less inclined attackers will be to attempt it. Also, the harder it is to break into the system, the more time you have to identify an attack and attempt to stop it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

the error states no operator ">>" matches these operand

Most likely you forgot to include the <string> header. That's the most common cause of this error.

the error states member "studentType::deptName (declared in studentType header file is inaccessible

Is deptName a private data member?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

So far I have this, but I think it is wrong because it is giving me syntax errors.

That's a reasonable conclusion. ;) What are the syntax errors?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

http://eternallyconfuzzled.com/arts/jsw_art_bigo.aspx

this is how it is exactly written on the handout

There's clearly a typo in the outer loop condition. i < n - 10 is probably what was intended.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Is salting passwords even necessary?

I consider it necessary and would question anyone who doesn't, given how easy it is to add a salt and the security boost of doing so.

iamthwee commented: Thanks for the feedback +14
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

lol.. okies.. now imthewee it's you turn.. come back.. where are you? we are waiting for you here. come on!!

Since you don't seem to be taking the hint, I'll lay it down in no uncertain terms. We know that you're nitin1 and you're doing a piss poor job of pretending that you're not. If you can't let things go, why did you delete your account in the first place?

If you deleted your account because you couldn't change your name, for future reference any moderator can do that for you if you ask. If you deleted your account in protest, I can assure you that nobody cares.

On a side note, every one of us has done or said something we regret, but you don't need to delete your account and start over. Despite how it may feel to you, your embarrassing moments are easily forgettable to everyone else. Unless you habitually repeat the same mistakes over and over, everyone will quickly forget about past transgressions because they're simply not that important.

p.s. No, I won't explain in further detail every word I've used in this post. :rolleyes:

iamthwee commented: Nods +0
nitin1 commented: i think you are correct! +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The removing duplicates part has to be within that function.

That's a stupid requirement.

Even should be within that outer for loop.

I'm not entirely sure what you mean by that, but I'll assume that it means you can't split the tasks into two operations, and that both the sort and removal of duplicates must be interleaved.

If that's the case then I'd recommend using a temporary array to hold the result, then copying that result into the source array. The simplest method conceptually would be to find the smallest item starting at index 0 for every iteration of the outer loop (which would be infinite). If a smallest item isn't found, then you break from the loop. If it is found, count the occurrances from index 0 and copy those values into the next open slot of the temporary array. To make sure that duplicates aren't processed multiple times, you can set the counter for each instance of the smallest value to something like -1.

So in pseudocode:

# Sort and remove duplicates
while true
    min = 0
    found = a[min].count >= 0

    # Find the first unprocessed smallest item
    for i = 1 to N
        if a[i].count < 0
            continue

        if a[min].count < 0 or a[i].value < a[min].value
            found = true
            min = i
    loop

    if not found
        break

    count = 0

    # Count occurrences
    for i = 0 to N
        if a[i].value = a[min].value
            a[i].count = -1 # Mark as processed
            count …
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Alternatively, and this would be my preferred option, you can use the KeyPress event to disable that key entirely:

private void textbox_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\'')
        e.Handled = true;
}
Mike Askew commented: The better option by far. +4
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The first part of that function is used to sort the words gathered in a text file from A to Z, the second part is meant to find any duplicate words

Pro tip: If the description of your function includes the word 'also', it should probably be refactored into multiple functions.

Your function does the work of two functions, and those two tasks are wildly different. Therefore there should be two functions: one to sort, and one to remove duplicates. Given a sorted collection, removing duplicates is trivial, so they can be run in sequence. If you do this then the task is greatly simplified from removing duplicates during the sort.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Okay, I'll bite. On the assumption that you won't simply turn in my code as if you wrote it, and because the Vigenere cipher is so shockingly simple, here's some working code:

#include <climits>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

/*
    @description:
        Increments value and wraps around to 0 when limit is reached.
*/
template <typename T, typename U>
T wrapped_increment(T value, U limit) { return ++value >= limit ? 0 : value; }

/*
    @description:
        Encodes 'src' using the key 'key' in a Vigenere cipher
        given an alphabet spanning the whole of the char type.
*/
string sucky_cipher(const string& src, const string& key, int max = CHAR_MAX + 1)
{
    string::size_type i = 0, j = 0;
    string result;

    /*
        Encode with the formula: 'C[i] = (M[i] + K[i]) mod N' where M
        is the plain source, K is the key, and N is the alphabet size
    */
    while (i != src.size()) {
        result.push_back((src[i++] + key[j]) % max);
        j = wrapped_increment(j, key.size());
    }

    return result;
}

/*
    @description:
        Decodes 'src' using the key 'key' in a Vigenere cipher
        given an alphabet spanning the whole of the char type.
*/
string sucky_decipher(const string& src, const string& key, int max = CHAR_MAX + 1)
{
    string::size_type i = 0, j = 0;
    string result;

    /*
        Decode with the formula: 'M[i] = (C[i] - K[i]) mod N' where C
        is the encoded source, K is the key, and N is the alphabet size
    */
    while (i != src.size()) …
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The buttons may only show posts which are currently negative or positive, so if someone voted down and someone else voted up, the net result would not be visible on either button.

Yup. The numbers show voting history on your account and the buttons retrieve currently positive or currently negative posts.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

We're going to chalk it up as impractical to fix, at least in any automated way. Feel free to report any posts you find to have truly egregious formatting and one of the mods can attempt to fix it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Honestly, I think having extra steps just to vote (ie. click to open the dialog, then click to vote) would turn people off to voting. The quickness of the present system encourages votes.

As for the confusion of which arrow the hover box is representing, perhaps adding an up or down arrow in that little box to make it more clear what you're doing?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Hover over an arrow to add a comment and reputation. Click the arrow just to vote. The arrow you click on or hover over determines if it's positive or negative.

Aside from making a dummy dialog (which would ultimately be less user friendly as popups usually are) instead of splitting the two options with a hover over, I don't think we can simplify it any further. Though I'm open to suggestions. :)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster
bool? my_var = null;

Of if you like being verbose:

Nullable<bool> my_var = null;
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

That's not C++, but the algorithm isn't exactly language specific. To simplify things I'd suggest a nested loop:

// Check the previous, current, and next row
for (int i = row - 1; i <= row + 1; ++i) {
    // Exclude invalid row indices
    if (i < 0 || i >= board_rows)
        continue;

    // Check the previous, current, and next column
    for (int j = col - 1; j <= col + 1; ++j) {
        // Exclude invalid column indices
        if (j < 0 || j >= board_cols)
            continue;

        // Increment if the cell is true, excluding self
        if (!(i == row && j == col) && board[i][j])
            ++count;
    }
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

This is crystal report in vb6 problem

I'll move it to the VB6 forum.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

can you tell what's that your MAGIC part is ? i am excited to know that magic of a magician here. ;)

Find two pointers of type T one step away. Convert those pointers to char* and subtract the larger one from the smaller one. The result will be the number of bytes between the two address, and thus the size of type T.

If you know how pointers work then it makes perfect sense. If you don't know how pointers work then no amount of me repeating that explanation will clarify things.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

This is really confusing as I dont know which is right and which is wrong, could use some help

They're both right. Your compiler is right because it's from 1991 and conformed to the C++ of the time. Your book is also right because it's reasonably current with the ISO C++ standard that was first introduced in 1998 and heavily updated in 2011.

If you want to learn C++ as it was two decades ago then get a book that was printed two decades ago. On the other hand, if you want to learn C++ in a way that would be useful in this millennium then dump your compiler and get a newer one.

Also I would appreciate it if anyone could suggest a better IDE for windows 7 32 bit

Everything is better. :D Try Visual C++, Code::Blocks, and CodeLite. The former uses Microsoft's compiler and the latter two use MinGW and GCC.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What else could you need to know?

The contents of the file and enough context to test your code, obviously. For example, how have you defined WordStruct? Please compose a small test program that's complete, then post it here along with a sample of the file you're using. Reproduction of the problem is the first step in troubleshooting.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

That's not C either, what language are you using?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

i am sorry to say that i didn't get your second line what you trying to say in that. can you explain it a little bit more ?

It would be helpful if you quoted the line you didn't understand so that I know which one you're talking about. But assuming it's the part where I explained how information is lost during compilation, you should probably read up on how compilers work to get more details. I'm happy to describe it, but when you're not willing to take something at face value (ie. information is lost during compilation), a good book on the subject would probably be more suitable than a much more concise forum post.

secondly, the macro which you have defined above, can you explain me how is it working.

Magic.

you have used 0 and casting over there.

The use of 0 is the part that's not strictly portable, it's a trick when you need a pointer for arithmetic but have no object to point to and no intention of dereferencing the pointer.

I explained the casting already, so I'd recommend your next point of research be pointer arithmetic and how the types of pointers affect the results. You can start by reading my first post in this thread more carefully as I describe the concept of a step size.

If you're still confused and need me to elaborate, please point out what you do understand and exactly which parts confuse …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

for example static int a; if then, i included this file in other file twice, then will it give error(redeclaration or something like that) ?

The long answer is maybe, but in terms of best practice I'd recommend assuming that the answer is yes. C supports a concept called tentative definitions. Basically as long as there's not an initializer for the object, you can have as many tentative definitions as you want and in the end only one of them will be used:

static int a;

/* All of these are fine and legal */
static int a;
static int a;
static int a;
static int a;

int main(void)
{
    return 0;
}

However, tentative definitions aren't a feature that one should generally take advantage of, which is why I think that one should be aware of them, but otherwise pretend they don't exist.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

By practical work do you mean jobs? If so then I'd say yes, but they're probably few and far between. If you mean tasks that require coding effort then absolutely, software security is a big deal.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Why a text box? That forces you to validate essentially unchecked input. If you don't want to use radio buttons, how about a dropdown menu with M or F?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The problem is that strtok() won't tell you when a token isn't present, so you need some extra logic to identify tokens either by special characteristics or by expected position. Comments are easy because they're always led by a semicolon, but the others can be tricky. For example, if both the label and instruction can be any string, how do you know if you got a label but no instruction or an instruction but no label?

You explained what you're trying to do quite nicely, but that wasn't what I asked for. I asked for details about the assembly format. I hope that your instructor didn't invent something ambiguous that's difficult to parse, because that would just be cruel.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What's the topic?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

why array size is different inside function solution1?

Because the "array" in solution1 isn't an array, it's a pointer. Don't be tricked by the array-like syntax of int array[] in a function parameter declaration, it's just syntactic sugar and equivalent to int *array.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

compile type operator ?

Compile time operator. It means that the compiler already has all of the information it needs while compiling the code. The vast majority of this information is discarded by the time the code actually runs, which puts you at a significant disadvantage when trying to simulate compile time operations.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

but if i get wrong in my answers or if there is any mistake in my answers, then ?

Then you'll probably be corrected by someone who recognizes the mistake and knows how to fix it. That's how answering questions can help you learn.

Will you ban me from this site ?

You have a very strange idea of how this forum works. We don't ban people unless they break our rules (and even then only after doing so multiple times), and our rules are common sense things like "don't break the law", "don't spam the forum", and "don't harrass other members".

If we banned anyone who made a mistake in an answer then there wouldn't be any members left. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Keep in mind that you won't be able to perfectly simulate sizeof because it's a compile time operator. But you can get somewhat close for the type syntax with pointer arithmetic:

#define SIZEOF(type) (int)((char*)((type*)0) - (char*)((type*)0 + 1))

It's not strictly portable, but I've yet to see a system where this doesn't work. The trick is understanding that pointer arithmetic uses step sizes that match the size of the type of the pointer. So a pointer to int will step by the size of an int, a pointer to double with step by the size of a double, etc... If you take two of those pointers set one step apart and then cast them to pointers to char (which always have a step size of 1), subtracting will give you the size of the original type.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

As i am just a beginner, Can i throw some light or can i give my answer on the questions which are posted on the daniweb.

Of course you're allowed to answer questions. That's one of the best ways to learn. However, please refrain from providing complete solutions to homework questions when the OP hasn't shown any real effort at solving it on their own.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

you can use freopen() to read or write data from/into the file

Why freopen() specifically? freopen() is actually a fairly rare function to need, usually being the choice for unusual stuff like redirecting standard streams from code or changing the open mode of an existing stream.

But I am lost at how to specifically take the first integer.

This isn't a complex problem. You need to add all of the necessary code after the file is opened:

main(int argc, char **argv) {
  float average;                  /* SET this variable to computed average */
  int small, second_small;      /* and these hold the smallest values */
  FILE *input_file;
  char filename[32];
  if ((argc == 2)&&(strcmp(argv[1],"-d")==0)) input_file = open_file();
  else {
     if (argc != 2) {printf("Usage: %s <filename>\n",argv[0]); exit(2);}
     if ((input_file = fopen(argv[1],"r")) == NULL) {
        printf("Unknown file: %s\n",argv[1]); exit(3);
     }

     /* Your code here */

  }

Which would probably consist of an else clause with declarations for the size of the array and the pointer for allocating memory. Then you can simply use fscanf() to read the size, and allocate the array:

main(int argc, char **argv) {
      float average;                  /* SET this variable to computed average */
      int small, second_small;      /* and these hold the smallest values */
      FILE *input_file;
      char filename[32];
      if ((argc == 2)&&(strcmp(argv[1],"-d")==0)) input_file = open_file();
      else {
         if (argc != 2) {printf("Usage: %s <filename>\n",argv[0]); exit(2);}
         if ((input_file = fopen(argv[1],"r")) == NULL) {
            printf("Unknown file: %s\n",argv[1]); exit(3);
         }
         else {
            int size;

            if (fscanf(input_file, …
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

C has no references, so you'd use a pointer to a pointer. The concept of using a pointer to an object to modify the object can be applied recursively, and the "object" you need to modify can be a pointer.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

instead of all that shifting I just store it in a char array then xmit the array

This introduces a portability issue that doesn't exist with shifting (ie. byte order), though whether that matters to the OP hasn't been established.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Do you think they might ask me about active directory and exhange server?

You might be asked about IIS, but I strongly doubt that a webmaster would be expected to maintain the local network if it's not explicitly stated that you're interviewing for a dual webmaster/sysadmin position.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

how to run the c program on note pad shall any one give me answer

You don't run a C program from notepad. Notepad is a text editor and nothing more. What you need to do is write your code with a text editor, then compile it using a compiler. The compiler may also include a linker which means the end result will be an executable file that can then be run.

C is a compiled language, so there are a few steps from source code to executable. The whole process is greatly simplified with an IDE, though the learning curve of figuring out how to prepare and build a project in an IDE is comparable to learning about the underlying steps of compilation and linking directly. ;)

Apparently my links have been broken to other people today, but hopefully this one works.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

If any admin or anybody has problem with me(as i am nothing in front of them), then they can ban me any time. thnaks.

That's not how it works. If you break the rules enough to accumulate too many infraction points, you'll be banned automatically by the system. Otherwise, you'll not be banned...ever.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

any response will be highly apprciated.

When you down vote every response for frivolous and trivial reasons, I'm disinclined to do so. Goodbye.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Try this one. :rolleyes:

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Excessive ass kissing is what you've been doing with me. Calling me a genius, always acting like only my responses are worth anything to you, and in general fawning over me. I know you're just trying to show respect, but it makes me uncomfortable.

Outright hostility is what you've been doing with WaltP. He may be gruff, but he's done nothing to hurt you or your threads, despite what you may think. If you don't like him or his posts, ignore him. That's what mature people will do on a public forum when no rules are broken. If he breaks a rule, report the post with the Flag Bad Post button.

I'm suggesting that you learn how to simply ignore insults, imagined or not. Try to be polite, but you don't have to treat people who help you like gods; doing so could easily turn them off from helping you in the future because it's often perceived as creepy.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

but when you come to know that he dont have any worth of you and even laugh when you show respect to them, then that is a pathetic situaion. what about this ?

You take it up privately with him instead of airing your dirty laundry. And if what you suspect turns out to be true, simply walk away and move on.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Can't we assign the value returned by asctime() to a string?

You can, but asctime() uses an internal buffer for the string, so it would be wise to make sure that a copy is made:

#include <cstring>
#include <ctime>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    time_t now = time(0);

    // Using a std::string (recommended)
    {
        string s = asctime(localtime(&now));

        cout << s;
    }

    // Using a C-style string
    {
        char *p = new char[50]; // Plenty of room

        strcpy(p, asctime(localtime(&now)));
        cout << p;

        delete[] p;
    }
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Some compilers will add a pragma that allows you to do this, usually with the name "pack".

Here's one for Visual Studio and one for GCC.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

After his posts, see nobody is replying in this post, not even npcomplete who was helping me a little bit.

After np_complete's posts, you appear to have done nothing except ask for more hints. Why should others try to help you when you clearly ignored the one who did? It has nothing to do with someone else "destroying" your thread, you've accomplished that yourself by making helpers feel unwelcome.

Excessive ass kissing is creepy. Outright hostility is repulsive. Maybe you should try somewhere in the middle.