# Thread: pseudocode algorithm in C++

1. ## 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
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.

2. Originally Posted by particlejohn
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
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
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
endfor
close file B
endwhile
RonL

3. Originally Posted by CaptainBlack
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
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
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) {
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)?

4. Originally Posted by particlejohn
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) {
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