c - What is wrong with my string generating program? -


i have program takes 7 arguments. first argument ignored. main function fcfsa takes 8 arguments : s1, s2, x1, y1, z1, x2, y2, z2. s1 , s2 char pointer variables , x1..z2 last 6 integer arguments in argv in consecutive order.

fcfsa should this: first string, s1, consist of x1 r's, followed y1 w's, followed z1 r's. second string, s2, consist of x1 r's, followed x2 r's, followed y2 w's, followed z2 r's.

but not getting correct output when executing program ./main 0 4 2 7 3 6 5 again first argument 0 ignored now.

this output:

inputs: 0 4 2 7 3 6 5  maxsize=27  part 1  rrrrwwrrrrrrrrrrrr+y? rrrrrrrwwwwww  0 4 2.0 0.86364 

and main.c:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include "pslibrary.h"  void part0(char *s1, char *s2); void display(char *heading, char *s1, char *s2); void fcfsa(char *s1, char *s2, int x1, int y1, int z1, int x2, int y2, int z2);  int main(int argc, char **argv) {     int i;     printf("assignment 0 program written marcus lorenzana\n");     if (argc != 8) {         printf("error. wrong number of arguments\n");         return 1;     }     printf("inputs: ");     (i = 1; < 8; i++) {         printf("%s ",argv[i]);     }     printf("\n");      //get maximum string size     int maxsize=0;     (i = 1; < 8; i++) {         maxsize+=atoi(argv[i]);     }     printf("maxsize=%d\n",maxsize);      char str1[maxsize],str2[maxsize];      fcfsa(str1,str2,atoi(argv[2]),atoi(argv[3]),atoi(argv[4]),atoi(argv[5]),atoi(argv[6]),atoi(argv[7]));    display("part 1\n",str1,str2);          return 0; } 

and program containing fcfsa:

#include <stdio.h> #include <string.h> #include "pslibrary.h"  void part0(char *s1, char *s2){     strcpy(s1,"rrwwwwwrrrrrrrrr");      strcpy(s2,"rrrrrrwwwwwwwwrrrrrrrrr");  }  void display(char *heading, char *s1, char *s2){     printf("\n");      printf("%s\n",heading);      printf("%s\n",s1);      printf("%s\n",s2);      printf("\n");      int s1len = strlen(s1);      int s2len = strlen(s2);      int i,s1cnt,s2cnt,s1cnt2,s2cnt2;     s1cnt=s2cnt=0;       s1cnt2=s2cnt2=0;     (i = 0; < s1len; i++) {         if (s1[i]=='r')             s1cnt++;      }     (i = 0; < s2len; i++) {         if (s2[i]=='r')             s2cnt++;      }     float average_r = (s1cnt+s2cnt)/2;       (i = 0; < s1len; i++) {         if (s1[i]=='r')             s1cnt2++;      }     (i = 0; < s2len; i++) {         if (s2[i]=='r')             s2cnt2++;      }      int longest;      if (s2len > s1len) {         longest = s2len;      } else {         longest = s1len;      }      float average_r = (float)(s1cnt2+s2cnt2)/longest;      printf("%d %d %.1f %.5f\n",s1cnt,s2cnt,average_r,average_r);  }  void fcfsa(char *s1, char *s2, int x1, int y1, int z1, int x2, int y2, int z2){     //s1: x1 r's, y1 w's, 0 or more r's, z1 r's     //s2: x1 r's, x2 r's, y2 w's, 0 or more r's, z2 r's      int i;         //s1 fill     int s1_start=0;         int s1_end=x1;       (i = s1_start; < s1_end; i++) {         s1[i]='r';     }      s1_start=s1_end;      s1_end+=y1;      (i = s1_start; < s1_end; i++) {         s1[i]='w';     }     s1_start=s1_end;     s1_end+=z1;     (i = s1_start; < s1_end; i++){         s1[i]='r';      }     s1[s1_end]='\0';         //printf("s1:%s\n",s1);          //s2 fill     int s2_start=0;     int s2_end=x1;     (i = s2_start; < s2_end; i++) {         s2[i]='r';     }     s2_start=s2_end;      s2_end+=x2;     (i = s2_start; < s2_end; i++) {         s2[i]='r';     }     s2_start=s2_end;     s2_end+=y2;     (i = s2_start; < s2_end; i++) {         s2[i]='w';     }     s2_start=s2_end;     s2_end+=z2;      (i = s2_start; < s2_end; i++) {         s1[i]='r';     }     s2[s2_end]='\0';     //printf("s2:%s\n",s2);  } 

the error in code:

s2_start=s2_end; s2_end+=z2;  (i = s2_start; < s2_end; i++) {     s1[i]='r';  // ^^^^^^^  error } s2[s2_end]='\0'; 

that line should be:

    s2[i]='r'; 

you can change coding habits avoid making mistakes this. example, divide fcfsa 2 functions.

void fcfsa(char *s1, char *s2, int x1, int y1, int z1, int x2, int y2, int z2){    fcfsa1(s1, x1, y1, z1);    fcfsa2(s2, x1, x2, y2, z2); } 

where

void fcfsa1(char *s, int x1, int y1, int z1){    //s: x1 r's, y1 w's, 0 or more r's, z1 r's    int i;    int start=0;    int end=x1;      (i = start; < end; i++) {       s[i]='r';    }      start=end;     end+=y1;     (i = start; < end; i++) {       s[i]='w';    }    start=end;    end+=z1;    (i = start; < end; i++){       s[i]='r';     }    s[end]='\0'; }  void fcfsa2(char *s, int x1, int x2, int y2, int z2){     //s: x1 r's, x2 r's, y2 w's, 0 or more r's, z2 r's      int i;     int start=0;     int end=x1;     (i = start; < end; i++) {         s[i]='r';     }     start=end;      end+=x2;     (i = start; < end; i++) {         s[i]='r';     }     start=end;     end+=y2;     (i = start; < end; i++) {         s[i]='w';     }     start=end;     end+=z2;      (i = start; < end; i++) {         s[i]='r';     }     s[end]='\0'; } 

you can further simplify code writing helper function fill array characters.

void fill(char *s, int start, int end, char c) {    int i;    (i = start; < end; i++) {       s[i]=c;    }  } 

then, fcfsa1 , fcfsa2 can simplified to:

void fcfsa1(char *s, int x1, int y1, int z1){    //s: x1 r's, y1 w's, 0 or more r's, z1 r's    int start=0;    int end=x1;      fill(s, start, end, 'r');     start=end;     end+=y1;     fill(s, start, end, 'w');     start=end;    end+=z1;    fill(s, start, end, 'r');     s[end]='\0'; }  void fcfsa2(char *s, int x1, int x2, int y2, int z2){     //s: x1 r's, x2 r's, y2 w's, 0 or more r's, z2 r's      int start=0;     int end=x1;     fill(s, start, end, 'r');      start=end;      end+=x2;     fill(s, start, end, 'r');      start=end;     end+=y2;     fill(s, start, end, 'w');      start=end;     end+=z2;      fill(s, start, end, 'r');      s[end]='\0'; } 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -