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

can any1 pls check this code and tell if ther's anythin wrong?

  • 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

    void copy(char **array,char name[10]) { FILE *fp; fp=fopen(name,"r"); char c; int i=0,j=0,flag=0,m=0; while((c=getc(fp))!='\n') { if(c==' ') { flag++; if(flag==1) { m=0; array[i][j]=' '; j++; } else if(flag==3) { j=0; array[i][j]='\0'; i++; flag=0; fseek(fp,-m,1); } } else { array[i][j]=c; j++; } m++; printf("%c %c\n",array[i][j],c); } fclose(fp); return; }

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

    it is supposes to copy groups of three words into a 2d array... @farmdawgnation

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

    ex: this is a test file this is a is a test a test file

    • one year ago
  4. farmdawgnation Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Are you seeing a problem with the code as is? Knowing what's wrong would help.

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

    like array[i][j] and c prints different characters and array[i][j] prints garbage values

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

    this is the total code if u wanna run #include<stdio.h> #include<string.h> #include<stdlib.h> //int length(char name[10]); char** makeArray(int n); void copy(char **,char name[10]); //void compare(char **array,char name[10],int x); //int count(char name[10]); main() { int k=0,x; char name[10]; printf("enter the name of the file:"); scanf("%s",name); int i,j,comp[10]; char **array; x=length(name); array=makeArray(x); copy(array,name); /*for(j=0;j<x/1000;j++) { printf("%s\n",array[j]); } /* compare(array,name,x);*/ } int length(char name[10]) { int x=0,max=0,j=0,flag=0,m; FILE *fp; fp=fopen(name,"r"); char c; while((c=getc(fp))!='\n') { // printf("flag:%d,m:%d,j:%d,max:%d,char:%c,x:%d\n",flag,m,j,max,c,x); if(c==' ') { flag++; if(max<j) max=j; if(flag==1) { m=0; } if(flag==3) { x++; if(max<j) max=j; j=0; flag=0; fseek(fp,-m,1); } } else { j++; } m++; } fclose(fp); x=x+1; printf("the number of phraes:%d,max size of word:%d\n",x,max); x=x*1000+max; printf("%d\n",x); return 1000*(x)+max ; } char** makeArray(int n) { int i; char **array; array=(char **)malloc((n/1000)*(sizeof(char *))); for(i=0;i<n;i++) { array[i]=(char *)malloc((n%1000)*sizeof(char)); } return array; } void copy(char **array,char name[10]) { FILE *fp; fp=fopen(name,"r"); char c; int i=0,j=0,flag=0,m=0; while((c=getc(fp))!='\n') { if(c==' ') { flag++; if(flag==1) { m=0; array[i][j]=' '; j++; } else if(flag==3) { j=0; array[i][j]='\0'; i++; flag=0; fseek(fp,-m,1); } } else { array[i][j]=c; j++; } m++; printf("%c %c\n",array[i][j],c); } fclose(fp); return; }

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

    You're never allocating memory for array for starters.

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

    Ah.

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

    i did and passed the array as an argument r8

    • one year ago
  10. farmdawgnation Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Yowza segmentation fault.

    • one year ago
  11. farmdawgnation Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    You're doing something you're not supposed to :P

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

    i dint get any segmentation fault

    • one year ago
  13. farmdawgnation Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    ``` enter the name of the file:testfile the number of phraes:3,max size of word:7 3007 ? t 2 h ? i ? s i s , a i s a t e s t a t e s t f i l e Segmentation fault: 11 ```

    • one year ago
  14. farmdawgnation Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    BTW. Single letter variable names suck. :P

    • one year ago
  15. farmdawgnation Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Yeah man, I'm not sure what's going on. I don't really feel like spending three hours with gdb digging into it to figure it out. I don't really have that much free time atm. :/

    • one year ago
  16. farmdawgnation Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    FWIW I don't see anything obvious.

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

    hmmm thanks anyway....if u find anythin lemme kno

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

    lol not a bigg thing....the print statement was showing some random values coz.... of j!!! ie the next character that's not copies yet!!

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

    nd that xplainds the seg fault too

    • 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.