Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

A.Avinash_Goutham Group Title

c prog prob

  • one year ago
  • one year ago

  • This Question is Closed
  1. A.Avinash_Goutham Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    @slotema

    • one year ago
    1 Attachment
  2. A.Avinash_Goutham Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    here's the task....it has to read the first line of the file and copy every word into a 2d array but for some reason.....the array is empty.....

    • one year ago
  3. A.Avinash_Goutham Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    i tried two methods initially find the no.of character's in the file in the line 1 and create a dynamic array and copy the contents in to it.......(linear_array) then method 1: copy every char in to array ...if a space is encountered increment row pointer of 2d array method 2 : replace every space in the linear_array and replace it with \0 then copy each string into each row both of them dint work the array is either empty or copies a single word here's the test file used gcc

    • one year ago
    1 Attachment
  4. slotema Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    You replace every space with '\0', Suppose the file contains "Hello, World". In your loop, you ask for strlen(p), which will be 6 on the first iteration (matching "Hello,"). This one is copied just fine. The second iteration however, p points to '\0' (what used to be the space). So strlen(p) is 0 the second iteration (and all other iterations). So strlen does NOT include the '\0'. Btw, don't forget to free the memory you malloced

    • one year ago
  5. A.Avinash_Goutham Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    no i need it later so i cant free it yet.......that's not the entire prog thanks....but y dint the other method work like copying the array char by char?

    • one year ago
  6. A.Avinash_Goutham Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    the one i commented out

    • one year ago
  7. A.Avinash_Goutham Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    • one year ago
    1 Attachment
  8. A.Avinash_Goutham Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    i think i got ur xplanation but when i used strlen(p) i got a result of 5 like strlen("\0askldghal"\0") should give a zero?

    • one year ago
  9. slotema Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    The problem with the commented code is the the else-part of the for-loop. You first increment k before assigning anything. Because of this, array[j][0] (the start of the string) will remain 0. strlen("\0askl...") will indeed give 0 length. You could also use the strtok function from the standard library to split a string into words (see: http://www.cplusplus.com/reference/cstring/strtok/ ).

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.