473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Nested loop holding up completion???

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 */
}
}
Nov 13 '05 #1
5 2193

Blankdraw <sp********@aol.com> wrote in message
news:9f**************************@posting.google.c om...
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


Nov 13 '05 #2
sp********@aol.com (Blankdraw) writes:
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++;
--
"You call this a *C* question? What the hell are you smoking?" --Kaz
Nov 13 '05 #3
Blankdraw wrote:
CBFalconer <cb********@yahoo.com> wrote in message
Besides the other comments, you still haven't posted a complete,
compilable source exhibiting your problem.
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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #4
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.
Nov 13 '05 #5
From: Blankdraw (sp********@aol.com)
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.
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: smauldin | last post by:
Why does the execution plan have a nested loop join for a simple select with an UDF in the where clause? Here is the query: select * from test_plan where vCol = my_udf('test') Here is the...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
1
by: Edward Diener | last post by:
I want to create a nested class within my component class. The nested class will have public properties. Naturally I want those properties to be serialized just as those properties of my component...
17
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html Why is C# 500% slower than C++ on Nested Loops ??? Will this problem be solved in...
9
by: Javaman59 | last post by:
Using local declarations within a block often makes code more readable, but is it less efficient? eg... void P() { while (...) { int i = ...; bool b = ...; .... } }
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.