13 Replies - 2358 Views - Last Post: 05 October 2012 - 03:23 AM Rate Topic: -----

#1 Akasen  Icon User is online

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 13-August 12

(C) Printing an array without a loop

Posted 04 October 2012 - 01:05 PM

Hello DIC's C and C++ community! My name is Akasen and I am here for guidance on a problem I am facing.

I am currently in the work of a project with a friend and he has happened to have given me a task to print numbers in an array with using "printf" multiple times.

This means that
    for(int j = 0; j < diceAmount; j++)
        printf("%d | ", diceResultList[j]);

is not acceptable as an answer. He has also specified that the usage of sprintf is applicable as well.

I am unsure of how to go about this problem and would like any help in getting a better understanding of how to solve this problem.

Thank you,
Akasen

Is This A Good Question/Topic? 0
  • +

Replies To: (C) Printing an array without a loop

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3165
  • View blog
  • Posts: 9,559
  • Joined: 05-May 12

Re: (C) Printing an array without a loop

Posted 04 October 2012 - 01:19 PM

Why? If this is for a project for work, you should always use the the most expedient and clear approach available to you. Unless you get paid by the lines of code, or the number of hours worked, or are looking for job security by being the only one who understands the code, why would you have this silly constraint of not using a loop, and not using printf() or sprintf().

itoa() (if your C runtime libary supports it) maybe an option, otherwise you'll have to roll you own. http://www.cplusplus...y/cstdlib/itoa/

Good luck rolling your own if the array of numbers is an array of floating point numbers and you need to print them out in scientific notation. It's doable, but just busy work, much like rolling your own itoa() will be busywork when sprintf() is readily available.
Was This Post Helpful? 0
  • +
  • -

#3 Akasen  Icon User is online

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 13-August 12

Re: (C) Printing an array without a loop

Posted 04 October 2012 - 01:28 PM

View PostSkydiver, on 04 October 2012 - 08:19 PM, said:

Why? If this is for a project for work, you should always use the the most expedient and clear approach available to you. Unless you get paid by the lines of code, or the number of hours worked, or are looking for job security by being the only one who understands the code, why would you have this silly constraint of not using a loop, and not using printf() or sprintf().

itoa() (if your C runtime libary supports it) maybe an option, otherwise you'll have to roll you own. http://www.cplusplus...y/cstdlib/itoa/

Good luck rolling your own if the array of numbers is an array of floating point numbers and you need to print them out in scientific notation. It's doable, but just busy work, much like rolling your own itoa() will be busywork when sprintf() is readily available.


I am allowed to use printf and sprintf.

Actually let me elaborate a bit on the project. I had brought up with my friend, who is knowledgeable in programming, the idea of doing a project in order to help teach myself, a person with little knowledge on programming, how to program. I had finally decided what I wanted to work on about two days ago. What did I decide? A dice rolling program.

So even though it is meant for learning, think of it more as a challenge to familiarize me with certain concepts. At least that's how I see it.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3165
  • View blog
  • Posts: 9,559
  • Joined: 05-May 12

Re: (C) Printing an array without a loop

Posted 04 October 2012 - 01:32 PM

If are allowed to use printf() or sprintf(), but not use loops, take a look at the concept of recursion: http://en.wikipedia....mputer_science)
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: (C) Printing an array without a loop

Posted 04 October 2012 - 01:41 PM

is recursion allowed?


The && operator can eliminate conditional if statements if the goal is to minimize the lines of code

This post has been edited by jjl: 04 October 2012 - 01:41 PM

Was This Post Helpful? 0
  • +
  • -

#6 Akasen  Icon User is online

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 13-August 12

Re: (C) Printing an array without a loop

Posted 04 October 2012 - 02:54 PM

Let me look back at prior chat logs and see if I can't give a better summary of what he wants.

I'll just post the general log

Alright I gotta go to bed now, but I have some homework for you. Make it print the individual dice results along with the total. The catch is, don't use printf for each dice. Put them all in one string.
[10/2/2012 10:10:05 PM] Archerer: 4, 5, 3, 8
[10/2/2012 10:10:08 PM] Archerer: Something like that
[10/2/2012 10:10:12 PM] Archerer: but make sure it's all in one string
[10/2/2012 10:10:18 PM] Archerer: and once you create the string, THEN you can printf it
[10/2/2012 10:10:22 PM] Akasen: You mean like one line?
[10/2/2012 10:10:24 PM] Akasen: Oh
[10/2/2012 10:10:25 PM] Archerer: yes
[10/2/2012 10:10:32 PM] Archerer: but you're only allowed to use printf one time
[10/2/2012 10:10:40 PM] Archerer: and that's after you've compiled the entire string
[10/2/2012 10:10:52 PM] Archerer: you CAN use sprintf, though
[10/2/2012 10:11:01 PM] Akasen: So something like making a list of numbers and then printing that list?
[10/2/2012 10:11:12 PM] Archerer: (I'm trying to teach you how to be able to output this to something other than the conio)
[10/2/2012 10:11:15 PM] Archerer: yes
[10/2/2012 10:11:18 PM] Archerer: Make the list first
[10/2/2012 10:11:20 PM] Archerer: then print the list
[10/2/2012 10:11:32 PM] Archerer: Don't print the list until the entire list is made
[10/2/2012 10:12:31 PM] Archerer: like I said
[10/2/2012 10:12:35 PM] Archerer: put the entire list in one string
[10/2/2012 10:12:53 PM] Archerer: then do printf("%s", diceResultList);
[10/2/2012 10:13:03 PM] Archerer: Follow me?
[10/2/2012 10:13:45 PM] Akasen: I get the idea.
[10/2/2012 10:13:48 PM] Archerer: Alright
[10/2/2012 10:13:55 PM] Archerer: Like I said, you're allowed to use sprintf
[10/2/2012 10:14:05 PM] Archerer: (that doesn't output to the conio)
[10/2/2012 10:14:39 PM] Archerer: Or you can use strcat
[10/2/2012 10:14:48 PM] Archerer: plus the integer to string conversion functions
[10/2/2012 10:14:55 PM] Archerer: I don't really care.
[10/2/2012 10:16:32 PM] Akasen: Alright, I'll let you go for now then. If I have any questions I will bring it up.


The general idea as I understand it is to print the results of dice rolls in one string, rather than having it in a loop that increase a variable like "i" for something like "diceResultList[i]" that prints out each part of an array.

Sorry for any confusion at this rate, I'm still trying to learn my way around things in C.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: (C) Printing an array without a loop

Posted 04 October 2012 - 02:59 PM

HINT: Look at the man page for sprintf, in particular the return value. How might you use that to accomplish your goal?
Was This Post Helpful? 0
  • +
  • -

#8 Akasen  Icon User is online

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 13-August 12

Re: (C) Printing an array without a loop

Posted 04 October 2012 - 07:41 PM

View PostJackOfAllTrades, on 04 October 2012 - 09:59 PM, said:

HINT: Look at the man page for sprintf, in particular the return value. How might you use that to accomplish your goal?

Reading on return value, "Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings)", that would be if I had the sentence "Hi there!". That sentence would be 9 characters long.

I was recently skyped by my friend checking up on me and telling me that I can use printf, like I know it really helps. He also mentions return values as well, along with allocating a large block of memory for the string and printing to it. And also something about offset, but really it's like he's spouting ideas at me like I'm suppose to have some epiphany to fix the problem. I'm out of epiphanies, everytime I felt like I had the answer, I find out it doesn't work.
Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: (C) Printing an array without a loop

Posted 04 October 2012 - 09:37 PM

His instructions seem pretty clean cut

Quote

and once you create the string, THEN you can printf it


You need to allocate a block of characters, add your values, then print it using printf.

char str[50];
strcpy(str, "This is the result:");

//add results

printf("%s", str);


Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3165
  • View blog
  • Posts: 9,559
  • Joined: 05-May 12

Re: (C) Printing an array without a loop

Posted 04 October 2012 - 09:41 PM

As I understand that chat log, you are allowed to call printf() only once, but there no restriction about using loops.

So in theory, you could something like this pseudo code:
make string empty
for(int i = 0; i < diceCount; i++)
    append dice[i] value to string
printf(string);



The point of the return value of sprintf() being signficant is because it tells you how many characters in printed out. You can use this to do some append logic.

Here is some basic appends using sprintf() with hard coded values that you can try out in your code:
int main()
{
    char buffer[256];

    // put "bat" starting at the beginning of the buffer
    sprintf(buffer, "bat");

    // put "man" starting at the 4th element of the buffer
    sprintf(&buffer[3], "man");

    printf(buffer);
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#11 Akasen  Icon User is online

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 13-August 12

Re: (C) Printing an array without a loop

Posted 04 October 2012 - 11:18 PM

At this point, I realized he didn't mean that I could not use a loop (Which now makes the topic title invalid).

At this moment, I have a piece of code that I am working on and am currently working with my friend to get to a solution.

It looks like this at the moment. (I will also put the initilizations I have
    char dice[64];
    unsigned diceSides, diceAmount;
    signed modifier = 0;
    int diceTotal = 0;
    int *diceResultList;
    char *dicePrintList;
    int offset;

    for(int j = 0; j < diceAmount; j++)
    {
         offset += sprintf(dicePrintList + offset*sizeof(char), "%d ", diceResultList[j]);
    }
    printf("%s\n", dicePrintList);

This post has been edited by Akasen: 04 October 2012 - 11:18 PM

Was This Post Helpful? 0
  • +
  • -

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: (C) Printing an array without a loop

Posted 05 October 2012 - 12:12 AM

offset is garbage on the first iteration of your loop, therefore garbage * sizeof(char) is also garbage. Make sure to initialize your value before you use it.
Was This Post Helpful? 1
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: (C) Printing an array without a loop

Posted 05 October 2012 - 02:30 AM

Where you're very close, you're making it a bit more complicated than it need be here.

offset += sprintf(dicePrintList + offset*sizeof(char), "%d ", diceResultList[j]);


This code demonstrates how to do this, and formats it nicely.
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int vals[] = { 5, 8, 7, 12, 4 };
    size_t i = 0, len = 0, count = sizeof(vals)/sizeof(vals[0]);
    char str[256] = { 0 };

    for (; i < count; ++i)
    {
        len += sprintf(&str[len], "%d%s", vals[i], i == count - 1 ? "\n" : ", ");
    }
    printf("Rolls: %s", str);
    return 0;
}


./diceroll
Rolls: 5, 8, 7, 12, 4

Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: (C) Printing an array without a loop

Posted 05 October 2012 - 03:23 AM

The sizeof(char) is always one, so you can ditch that. Also, pointer arithmetic will take care of sizing, so be careful with that.

I like the kind of pointer thing in your code, as opposed to indexes. Also, time to start thinking functions.

e.g.
#include <stdio.h>

char *arrayToStr(char *buff, int *a, int size) {
	char *s = buff;
	for(;size>0; size--, a++) { s += sprintf(s, "%d ", *a); }
	return buff;
}

// stealing Jack's main
int main() {
	char str[256];
	int vals[] = { 5, 8, 7, 12, 4 };
	
	printf("Rolls: %s\n", arrayToStr(str, vals, sizeof(vals)/sizeof(vals[0])));
	return 0;
}



The function mimics what you had in mind, I think.

Hope this helps.

This post has been edited by baavgai: 05 October 2012 - 03:24 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1