Results 1 to 4 of 4

Math Help - pseudocode algorithm in C++

  1. #1
    Member
    Joined
    Jun 2008
    Posts
    170

    pseudocode algorithm in C++

    I have text file of names and another file with numbers associated to each file. I want to open each file, and perform data analysis on a specific column.

    Call the files A(the names) and B(data associated with each file). Here is the pseudo code:

    Code:
     rewind file pointer back to beginning of file A. 
              loop through file A and open each file
                      count nrows in file B. 
                      loop through first value to nrows-32
                         read third column
                      store values in an array
    Is this the correct process for accessing a specific number of rows in a specific column? Do I need to store the values in a buffer?

    I have written a function that "accesses a specific column."

    Thanks

    Note: the indentation in the pseudo code is for the organization of the loops.
    Last edited by particlejohn; July 5th 2008 at 11:05 AM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by particlejohn View Post
    I have text file of names and another file with numbers associated to each file. I want to open each file, and perform data analysis on a specific column.

    Call the files A(the names) and B(data associated with each file). Here is the pseudo code:

    Code:
     rewind file pointer back to beginning of file A. 
              loop through file A and open each file
                      count nrows in file B. 
                      loop through first value to nrows-32
                         read third column
                      store values in an array
    Is this the correct process for accessing a specific number of rows in a specific column? Do I need to store the values in a buffer?

    I have written a function that "accesses a specific column."

    Thanks

    Note: the indentation in the pseudo code is for the organization of the loops.
    This pseudo-code is not clear.

    Presumably file A is already open since you rewind it, in which case you will need either to open or rewing the files you are reading from.

    Best have some named receiving place for the read data (a buffer)

    Code:
     
      declare buffer[buffersize]
      rewind file A
      while not eof(A)
        read file name B from A
        open file B
        skip to row nrows in B  //next read will be from the start of row nrows
        for {idx=0; idx<nrows-32;idx++}  //dont like the look of the row count here
          buffer(idx)=read 3rd colum from current row of B
          skip to next row of B
        endfor
        close file B
      endwhile
    RonL
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Member
    Joined
    Jun 2008
    Posts
    170
    Quote Originally Posted by CaptainBlack View Post
    This pseudo-code is not clear.

    Presumably file A is already open since you rewind it, in which case you will need either to open or rewing the files you are reading from.

    Best have some named receiving place for the read data (a buffer)

    Code:
     
      declare buffer[buffersize]
      rewind file A
      while not eof(A)
        read file name B from A
        open file B
        skip to row nrows in B  //next read will be from the start of row nrows
        for {idx=0; idx<nrows-32;idx++}  //dont like the look of the row count here
          buffer(idx)=read 3rd colum from current row of B
          skip to next row of B
        endfor
        close file B
      endwhile
    RonL
    Thanks. Here is the code I wrote (without the header):
    Code:
     
        if (argc != 2){
            printf("Usage: CreateSummaryFile globalpath\n");
            exit(1);
        strcpy(globalpath,argv[1]);  // copies the command line string that you input into the array globalpath
    
        strcpy(Afile,globalpath);
        strcat(Afile,A);
    
    
        if ((Afptr = fopen(A,"r")) == NULL) {
            printf("Can't open %s  for reading!\n",Afile);
            exit(1); 
        }
    
        n= 0;
        while(!feof(Afptr)) {
            if (fgets(buf,MAX_CHAR,Afptr) == NULL)
                break;
            else {
                n++;
            }
        }
    
        } rewind(Afptr);
        printf("number in A = %d\n",n);
    
        while(!feof(Afptr)) {
            if (fgets(buf,MAX_CHAR,Aptr) == NULL)
                break;
            else {
                cp = finds(1,buf);
                strcpy(tempbuf,cp);
                sscanf(tempbuf,"%s",Aname);
                strcat(Aname, "_data.txt");
                printf("A name = %s\n",Aname);
            }
        }
        fclose(Afptr);
    }
    So the file was already open, that is why I had to rewind it. But if I want to perform data analysis on the third column, would it be best to create a new string vector (because the values are lost if you keep the original buffer)?
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by particlejohn View Post
    Thanks. Here is the code I wrote (without the header):
    Code:
     
        if (argc != 2){
            printf("Usage: CreateSummaryFile globalpath\n");
            exit(1);
        strcpy(globalpath,argv[1]);  // copies the command line string that you input into the array globalpath
     
        strcpy(Afile,globalpath);
        strcat(Afile,A);
     
     
        if ((Afptr = fopen(A,"r")) == NULL) {
            printf("Can't open %s  for reading!\n",Afile);
            exit(1); 
        }
     
        n= 0;
        while(!feof(Afptr)) {
            if (fgets(buf,MAX_CHAR,Afptr) == NULL)
                break;
            else {
                n++;
            }
        }
     
        } rewind(Afptr);
        printf("number in A = %d\n",n);
     
        while(!feof(Afptr)) {
            if (fgets(buf,MAX_CHAR,Aptr) == NULL)
                break;
            else {
                cp = finds(1,buf);
                strcpy(tempbuf,cp);
                sscanf(tempbuf,"%s",Aname);
                strcat(Aname, "_data.txt");
                printf("A name = %s\n",Aname);
            }
        }
        fclose(Afptr);
    }
    So the file was already open, that is why I had to rewind it. But if I want to perform data analysis on the third column, would it be best to create a new string vector (because the values are lost if you keep the original buffer)?
    You will need somewhere that is in some way persistent to store the data, so without detailed knowlege of what you are trying to do I would say yes you need to create some data structure to store the data, and if a string array is a suitable structure use it.

    RonL
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Algorithm help
    Posted in the Discrete Math Forum
    Replies: 6
    Last Post: November 12th 2009, 04:10 AM
  2. Pseudocode
    Posted in the Calculators Forum
    Replies: 1
    Last Post: April 25th 2009, 12:03 AM
  3. Algorithm pseudocode
    Posted in the Discrete Math Forum
    Replies: 0
    Last Post: October 16th 2008, 01:46 PM
  4. Algorithm
    Posted in the Discrete Math Forum
    Replies: 3
    Last Post: February 26th 2007, 05:42 AM
  5. Pseudocode
    Posted in the Number Theory Forum
    Replies: 7
    Last Post: September 28th 2006, 06:45 PM

Search Tags


/mathhelpforum @mathhelpforum