473,396 Members | 2,013 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,396 software developers and data experts.

simple output problem

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;
}

Mar 9 '06 #1
3 1629
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.
Mar 9 '06 #2

"RadiationX" <he*********@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro 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
Mar 9 '06 #3
"RadiationX" <he*********@gmail.com> 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...
Mar 9 '06 #4

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

Similar topics

4
by: Gauthier | last post by:
Hi, I've a simple issue with the use of extension objects. I'm trying to call a text formating method from an object that I add to my arguments collection, this method take an input string and...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
6
by: sathyashrayan | last post by:
Dear group, Following is a exercise from a book called "Oreilly's practical C programming". I just wanted to do a couple of C programming exercise. I do have K and R book, but let me try some...
11
by: Kush | last post by:
Hello All, I am having problem writing some data to file and I do not understand my mistake / misconception. I am trying to write some simple text to file but the text is rather output to...
11
by: Ground21 | last post by:
Hello. I'm new // sorry for my english :) I have to write simple program i c++. But I don't know how to code src - program should know it's name (unit1.exe so the name is "unit1" or "unit1.exe"...
15
by: Bjoern Schliessmann | last post by:
Hello all, I'm trying to simulate simple electric logic (asynchronous) circuits. By "simple" I mean that I only want to know if I have "current" or "no current" (it's quite digital) and the only...
3
by: Patrick | last post by:
Hi there, I have a simple problem to solve but somehow I am too stupid to figure out where is the error. Basically, I wanna shift each element of the array by an amount of n, whereas the n...
5
by: Timo | last post by:
I haven't been using ANSI-C for string parsing for some time, so even this simple task is problematic: I have a string tmp_str, which includes date + time + newline in format: "25.6.2008 21:49"....
1
by: jerry | last post by:
i have written a simple phonebook program,i'll show you some of the codes,the program's head file is member.h . i suppose the head file works well.so i don't post it. here's the clips of main...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.