Nested loop holding up completion???

B

Blankdraw

I can't get this nested loop to break the outer loop at the
5th data value so control can proceed to the next array col
and continue pigeon-holing the next 5 in its own column.

Why can I not get this nested loop to make sense?
This is the last holdup to completion of my silly project.
Explaining it from the inside - out, I want to fill a
120-col X 40-row array with data from a file containing
120 data records of 5 2-digit entries per rec.
A 52-iteration loop needs to go down one column at a time,
copying the previous value and incrementing the ones which
which are as far down the array as the data values 'say' so.
(rows are numbered 1-40 & data values are between 1 and 40.)
Help on this is desperately needed - my mind may not be logical
enough for this algorithm - mental block or something.
No need to comment on exact loop quantities, if I garbled any.

for (cycle = 1; cycle < 121; cycle++) /* 120 data recs */
{
fscanf(fileptr, "%i", &n); /* <---get next data value */

for (row = 0; row < 40; row++)
{
current[row][col] = current[row][col - 1];
if (n == row)
{
temp = current[row][col] + 1;
current[row][col] = temp; /* current[r][c]++ ??? */
}
}

EOL = floor(cycle / 5);
if ( EOL == (cycle / 5 ) ) { /* cycles 1 to 5 goto col 1 */
col++; /* note: cycle #0/5 -> NoGo */
/* next 5 (#6 - #10) goto col 2 */
}
}
 
B

Ben Pfaff

EOL = floor(cycle / 5);
if ( EOL == (cycle / 5 ) ) { /* cycles 1 to 5 goto col 1 */
col++; /* note: cycle #0/5 -> NoGo */
/* next 5 (#6 - #10) goto col 2 */
}

This code is confusing and probably confused, too. First, how is
`cycle' declared? If it has an integer type, then `floor(cycle /
5)' is just a clumsy way to write `cycle / 5' (since `cycle' is
always nonnegative). In that case, the `if' condition should
always be true.

Second, what is EOL? Is it a variable? If so, then why does it
have an all-caps name? Most C programmers reserve all-caps names
for macros and enumeration constants.

Do you really want the following or something similar?
if (cycle % 5 == 0)
col++;
 
M

Mike Wahler

Blankdraw said:
I can't get this nested loop to break the outer loop at the
5th data value so control can proceed to the next array col
and continue pigeon-holing the next 5 in its own column.

Why can I not get this nested loop to make sense?
This is the last holdup to completion of my silly project.
Explaining it from the inside - out, I want to fill a
120-col X 40-row array with data from a file containing
120 data records of 5 2-digit entries per rec.

First note that C arrays use 'row major' order.
A 52-iteration loop needs to go down one column at a time,

Why 52? Do you have 40 rows, or 52? Or 120 (considering
what I said above)?
copying the previous value and incrementing the ones which
which are as far down the array as the data values 'say' so.
(rows are numbered 1-40 & data values are between 1 and 40.)
Help on this is desperately needed - my mind may not be logical
enough for this algorithm - mental block or something.
No need to comment on exact loop quantities, if I garbled any.

for (cycle = 1; cycle < 121; cycle++) /* 120 data recs */

Used with a C '2-dimensional' array, each 'record' would
be a 'row' (first dimension) of the array. The 'fields'
would be the 'columns'.

If you want to 'prematurely' terminate an 'inner' loop,
you can force it to by assigning the loop counter to
the 'max' value indicated by the inner loop:

for(row = 0; row < rows; ++row)
{
for(col = 0; col < cols; ++col)
{
/* do whatever */
if(some_condition)
col = cols; /* force early 'col' loop termination */
}
}

Some folks use a 'goto' to do this, especially when
the code complexity does not allow such a 'clean'
solution.

-Mike
 
C

CBFalconer

Blankdraw said:
I can't get this nested loop to break the outer loop at the
5th data value so control can proceed to the next array col
and continue pigeon-holing the next 5 in its own column.
... snip ...

Besides the other comments, you still haven't posted a complete,
compilable source exhibiting your problem.
 
C

CBFalconer

Blankdraw said:
I assume this means someone thinks having the full code listing will
be of benefit in resolving the output-column increment problem....so:

I am going to regret this, but the first thing I did was reformat
your source into something with consistent indentation and
eliminate the double spacing of each line, which is pure
foolishness.

The result does indeed compile, with the following warnings:

[1] c:\c\junk>gcc junk.c
junk.c: In function `main':
junk.c:8: warning: missing braces around initializer
junk.c:8: warning: (near initialization for `current[0]')
junk.c:19:46: warning: "/*" within comment
junk.c:10: warning: unused variable `next'
#include <stdio.h>
#include <stdlib.h>
#include <process.h>

There is no such include file in standard C. There appears to be
a flaw in gcc 3.2.1 for DJGPP which allows this to go
unchallenged.
#include <math.h>

What do you want this for?
int main()

Use "int main(void)" or "int main(int argc, char **argv)". You
didn't omit the int return type for main, which is good.
{
int current[52][121] = {0}; /* 52 rows x 120 active columns */
int n, temp;
int next=1, row=0, col=1, cycle=0;
double EOL; /* End-Of-Line */

What in heavens name is this for?
FILE *fileptr;

/* point to a data value */
if ((fileptr = fopen("test_data.txt", "r")) == NULL ) {
printf("Error: Cannot open input file\n");
exit(0);

It might be better to signal an error with "exit(EXIT_FAILURE)",
or "return(EXIT_FAILURE)", which you can do since you included
stdlib.h.
}
/* while (!feof(fileptr)) /* compare data from fscanf - below */
for (cycle = 1; cycle < 7; cycle++) /* 5 entries x 120 data recs */

This will use cycle values of 1 through 6, for 6 entries. I see
no relationship to the comments.
{ /* use 7 test values */
fscanf(fileptr, "%i", &n); /* <---get next data value */

I doubt you really want fscanf here. You omitted to include the
content of test_data.txt, so we have no idea what you are trying
to input.

Things now degenerate into total confusion. You need to break
your code up into better modules. See end.
for (row = 0; row < 51; row++) /* 52 rows = 52 possibilities */
{
current[row][col] = current[row][col - 1];
if (n == row)
{
temp = current[row][col] + 1;
current[row][col] = temp; /* current[r][c]++ ??? */
}
}
EOL = floor(cycle / 5);
if ( EOL == (cycle / 5 ) ) { /* cycles 0 to 4 goto col 1 */
col++; /* note: cycle #0 / 5 -> NoGo */
/* next 5 (#6 - #10) goto col 2 */
}
}
for (row = 0; row < 52; row++) {
printf("\n");
for (col = 1; col < 121; col++) {
printf ("%i ", current[row][col]);
}
}
/* for (row = 0; row < 52; row++) */
/* { */
/* printf("\n"); */
/* for (col = 0; col < 120; col++) { */
/* printf ("%i ", current[row][col]); */
/* } */
/* } */

fclose(fileptr);
if ((fileptr = fopen("pb.txt", "w")) == NULL ) /* pb.txt is pb.out */
{
printf("Error: Cannot open input file\n");
exit(0);
}
/* while (!feof(fileptr)) */
for (row = 0; row < 52; row++) { /* dump array into text file */
fprintf (fileptr, "\n");
for (col = 1; col < 121; col++) {
fprintf (fileptr, "%2d ", current[row][col]);
}
}
fclose(fileptr);
return 0;
}

Strip out code that has been commented out. It does nothing but
add confusion.

I suggest you start over with a better organization:

/* the necessary includes */

/* Some #defines for any numbers you use other than 1 */

int fillcurrent(int *current, FILE *fp)
{
/* code this, return 0 for failure */
}

int dumpcurrent(int *current, FILE *fp)
{
/* code this, return 0 for failure */
}

int main(int argc, char **argv)
{
/* some variables, including current and filepointers */

/* code to open input and output files
using the command line args to supply names */

/* a call to a function to fill the 'current' array */
if (fillcurrent(current, fpin) {
/* a call to a function to dump the 'current' array */
if (dumpcurrent(current, fpout) {
return 0; /* success */
}
}
return EXIT_FAILURE;
}

and read the C faq, especially about multi dimensional array and
how to pass and manipulate them. If you don't like my suggestions
for error returns, change them.
 
B

Blankdraw

Several responses have suggested that formatted I/O should not be
used.

Initially when I posted to this board (months? ago) I said I much
preferred low-level I/O commands for this because I suspected that
those will stumble on end-of-line characters. This could be useful,
since recogizing the end-of-lines in the input data file is critical
to the case in general. If there is a way to make fscanf() do the
requirements of: reading through an integer data file (2-digit
integers); and jump loop levels at every end-of-line, then this would
suggest a rather nice rewrite. The data has to be handled as
numerical, because interpretting numbers according to their ASCII
precedence would make me a nervous wreck. I think I have a way
around the e-o-l problem, per my listing. When I did, a while back,
try to get help in terms of low-level functions, EVERYONE **shouted**
that that's for suckas and that I really need to step up to a higher
level of facility available in ANSI C. Maybe so. I am having
trouble walking while I am still learning to roll over onto my belly,
as it were.
 
B

Blankdraw

From: Blankdraw ([email protected])
Subject: Re: Nested ...crud - forgot another rationale: This is the
only article in this thread
View: Original FormatNewsgroups: comp.lang.c
Date: 2003-08-22 14:28:46 PST

(I misdirected this posting yesterday - resuming current thread...)
Several responses have suggested that formatted I/O should not be
used.

Initially when I posted to this board (months? ago) I said I much
preferred low-level I/O commands for this because I suspected that
those will stumble on end-of-line characters. This could be useful,
since recogizing the end-of-lines in the input data file is critical
to the case in general. If there is a way to make fscanf() do the
requirements of: reading through an integer data file (2-digit
integers); and jump loop levels at every end-of-line, then this would
suggest a rather nice rewrite. The data has to be handled as
numerical, because interpretting numbers according to their ASCII
precedence is too illogical, I think. I think I have a way
around the e-o-l problem, per my listing. When I did, a while back,
try to get help in terms of low-level functions, EVERYONE **shouted**
that that's for suckas and that I really need to step up to a higher
level of facility available in ANSI C. Maybe so. I am having
trouble walking while I am still learning to roll over onto my belly,
as it were.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top