Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > simple output problem

Reply
Thread Tools

simple output problem

 
 
RadiationX
Guest
Posts: n/a
 
      03-09-2006
Hello, I have a simple print output questioin. What I'm trying to do is
print an array of lenth 20 which holds 20 ints in the form of five
columns and four rows. like this below

12 80 90 6 7
1 6 99 8 9
8 3 5 3 12
9 4 2 2 15


I have no idea how to print it like this. Below is what I have so far.
It's wrong but I think I'm on the right track. What the code below does
is print one column broken up into five parts. I really would
appreciate some help.



#include <stdio.h>
#include <stdlib.h>
#include<time.h>
int random();


int main()
{
int j;
int user;
int k;

int golf[20]= {0};
int b;
srand(time(NULL));

// printf("%s%13s\n", "Element", "Value" );

for ( k = 0; k< 20; k++ )
{
b = random();
golf[20] = b;

if(k%4==0)
{
printf("\n");
}


printf("%8d\n", golf[20]);
}






system("PAUSE");
return 0;
}

int random()
{
int x;

x = 1 + rand()%99;
return x;
}

 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      03-09-2006
RadiationX schrieb:
> Hello, I have a simple print output questioin. What I'm trying to do is
> print an array of lenth 20 which holds 20 ints in the form of five
> columns and four rows. like this below
>
> 12 80 90 6 7
> 1 6 99 8 9
> 8 3 5 3 12
> 9 4 2 2 15
>
> I have no idea how to print it like this. Below is what I have so far.
> It's wrong but I think I'm on the right track. What the code below does
> is print one column broken up into five parts. I really would
> appreciate some help.
>
>
>
> #include <stdio.h>
> #include <stdlib.h>
> #include<time.h>
> int random();


Use full prototypes:
int random (void);
This helps your compiler warn you if you give a wrong
argument list.

> int main()


Mainly a matter of style:
int main (void)

> {
> int j;
> int user;
> int k;
>
> int golf[20]= {0};
> int b;
> srand(time(NULL));
>
> // printf("%s%13s\n", "Element", "Value" );
>
> for ( k = 0; k< 20; k++ )
> {
> b = random();
> golf[20] = b;
>
> if(k%4==0)
> {
> printf("\n");
> }
>
>
> printf("%8d\n", golf[20]);
> }


What you are doing at the moment is do two things at a
time.

Split this into two things:
1. Populating the array
2. Printing it

Ideally, make these functions of their own:
#define GOLF_SIZE 20
#define NUM_COLUMNS 5

int main (void)
{
int golf[GOLF_SIZE] = {0};

populateArray(golf, GOLF_SIZE);
printArray(golf, GOLF_SIZE, NUM_COLUMNS);

#ifdef USE_PAUSE
/* Whatever happens in system() is not topical to comp.lang.c */
system("PAUSE");
#endif

return 0;
}

where we have
void populateArray(int *array, int size);
void printArray(int *array, int size, int columns);
If you like the conceptual cleanness, use size_t instead
of int as type for the size and column number parameters.

>
>
>
>
>
>
> system("PAUSE");
> return 0;
> }
>
> int random()
> {
> int x;
>
> x = 1 + rand()%99;
> return x;
> }


There are better ways to implement random but I will not
dwell on that.

Let us go back to your question:
void printArray(int *array, int size, int columns)
{
int rowcount, colcount, rows, rest;

if ((NULL == array) || (size < 0) || (columns <= 0))
{
/* insert your error handling here */
return;
}

rows = size/columns;
rest = size%columns;

/* print the full rows */
for (rowcount = 0; rowcount < rows; rowcount++)
{
for (colcount = 0; colcount < columns; colcount++)
{
printf("%3d\t", array[rowcount*columns + colcount]);
}
putchar('\n');
}
/* Now for the rest*/
for (colcount = 0; colcount < rest; colcount++)
{
printf("%3d\t", array[rows*columns + colcount]);
}
putchar('\n');
}
The function is not tested but I think you get the
concept.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      03-09-2006

"RadiationX" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hello, I have a simple print output questioin. What I'm trying to do is
> print an array of lenth 20 which holds 20 ints in the form of five
> columns and four rows. like this below
>
> 12 80 90 6 7
> 1 6 99 8 9
> 8 3 5 3 12
> 9 4 2 2 15
>
>
> I have no idea how to print it like this. Below is what I have so far.
> It's wrong but I think I'm on the right track. What the code below does
> is print one column broken up into five parts. I really would
> appreciate some help.
>
>
>
> #include <stdio.h>
> #include <stdlib.h>
> #include<time.h>
> int random();
>
>
> int main()
> {
> int j;
> int user;
> int k;
>
> int golf[20]= {0};
> int b;
> srand(time(NULL));
>
> // printf("%s%13s\n", "Element", "Value" );
>
> for ( k = 0; k< 20; k++ )
> {
> b = random();
> golf[20] = b;

Bad news - attempt to store a value outside the bounds of golf.
Anything can happen.
>
> if(k%4==0)
> {
> printf("\n");
> }
>
>
> printf("%8d\n", golf[20]);

Why are you using golf array? What's wrong with just "b"?
> }
>
> system("PAUSE");
> return 0;
> }
>
> int random()
> {
> int x;
>
> x = 1 + rand()%99;
> return x;
> }
>


#define RANGE 20
#define COLUMNS 5

for ( k=0; k > RANGE; k++) {
b = random();
printf( "%-8d", b);
if ( k%COLUMNS == (COLUMNS-1) ) {
printf("\n");
}
}

For right-justified values, use "8d" instead of "%-8d".
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project


 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      03-09-2006
"RadiationX" <(E-Mail Removed)> writes:

> #include <stdio.h>
> #include <stdlib.h>
> #include<time.h>
> int random();


int random(void);

>
>
> int main()


int main(void);

> {
> int j;
> int user;
> int k;
>
> int golf[20]= {0};
> int b;
> srand(time(NULL));
>
> // printf("%s%13s\n", "Element", "Value" );
>
> for ( k = 0; k< 20; k++ )


"Magic numbers" should be discouraged. What happens if you want to
expirement with a 24-element array? or a 16-element array? This for
loop would stop working, unless you remember to change the number
here, too. You should probably use a macro:

#define NUM_ELEMS 20

> {
> b = random();
> golf[20] = b;
>
> if(k%4==0)
> {
> printf("\n");
> }
>
>
> printf("%8d\n", golf[20]);
> }


There's no need to populate golf[] here (which, as someone else
pointed out, you're doing wrong anyway: you're actually writing one
past the end of golf[]). However, assuming this is a homework
assignment where you actually have to have an array that you print
out, I recommend that you use two loops: one to populate the array
using random(), and another to print it out. Ideally, these could be
in two separate functions.

To fix the off-by-one thing, you'll pretty much want to use golf[k]
where you've been using golf[20].

Just copy the loop into two functions, take the "if (k%4)" and
printf()s out of the first one, and the assignments out of the other,
and you should be golden. The functions could look like:

void populate_array(int *golf);
void print_array(int *golf);

Use the defined macro in the loops for these functions, or else pass
in the size of the array as well.


> system("PAUSE");


My system doesn't have the above, and yours very likely doesn't need
it. In windows, you can configure your command window to not close
automatically when the program exits (don't ask me how--read your
documentation).

> return 0;
> }
>
> int random()
> {
> int x;
>
> x = 1 + rand()%99;
> return x;
> }


The vast majority of rand() implementations have very poor randomness
in the lower bits: it is usually recommended that you use something like:

x = 1.0 + (99.0*rand()/(RAND_MAX+1.0));

....not that it matters a whole lot for your purposes...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange problem - page output contains output from another request Paul ASP .Net 1 04-10-2007 03:41 PM
Simple output problem RadiationX C Programming 13 02-06-2006 05:02 AM
Sony Precision Cinema Progressive Output vs Component 480p Output Otto Pylot DVD Video 1 04-18-2004 09:49 PM
Is Fuji S3000 3.2m/pixel output, or 6 m/pixel interpolated output? Peter H Digital Photography 43 12-04-2003 02:35 PM
Output / Debug window output bug? John Bentley ASP .Net 0 09-10-2003 07:38 AM



Advertisments