tags:

views:

134

answers:

7

How to printf results from a loop?

For example if i have this something simple like this:

k[0]=2;
k[1]=3;
k[2]=4;

for (i = 0 ; i <= 2 ; i++)
{
    x[i]=5*k[i];
}

How do I print out the results for x[0],x[1],x[2] without having to keep repeating the array in printf? As in

printf("%d %d %d\n",x[0],x[1],x[2]);

I dont really want to do the printf above because for my problem i actally have an array of 100 values, i cant be repeating the x[0],x[1]... for one hundred times.

Hope someone can help out thanks loads!

+8  A: 

Maybe this:

for (j = 0; j < 100; j++) {
    printf ("%d ",x[j]);
}
printf ("\n");
Kinopiko
+3  A: 

You can put a printf in a loop. If you don't put a "\n" in the printf, the next printf will be on the same line. Then when you are done with the loop, printf just a newline.

There is no printf type specifier for an array, you have to loop through the array elements and print them one by one.

Lou Franco
A: 

You can think of doing it this way:

k[0]=2;
k[1]=3;
k[2]=4;

for (i = 0 ; i <= 2 ; i++)
{
    x[i]=5*k[i];
    printf("%d",x[i]);
}
printf("\n");
Kangkan
The result won't be the same, all numbers being on different lines
Grimmy
Just updated my answer to take care of your concern.
Kangkan
+2  A: 

Why can't you print them out as you go around the loop?

for (i = 0 ; i <= 2 ; i++)
{
    x[i]=5*k[i];
    printf("%d ", x[i]);
}
printf("\n");

Other than that you could have another loop underneath which strcats the results together into a single string for output.

locka
sorry my bad, actually my real problem revolves around an array of values that are defined by pointers ><, which kinda make things complicated i think..
esther
You can print out values in pointers by prefixing the value with a *. i.e. "int a = 5; int *b = printf("%d", *b);" will print 5. If your array was of pointers, then similarly ("int *data[10]; data[3] = malloc(sizeof(int)); *data[3] = 123; printf("%d", *data[3]);"
locka
heya locka, thanks...i cna feel a solution coming..urm, can u please explain a little more on the malloc part? thanks!
esther
malloc() stands for memory allocate. As your question was tagged C, I said malloc() however if your app is C++ you could also express the same above as "int *data[10]; data[3] = new int(1234); printf("%d", *data[3]);". The "new int(1234)" allocates an integer and assigns 1234 to it via its default constructor. If you don't know much about memory allocation, it is worth reading up on it. Be aware that anything you allocate must be freed. For malloc() that means calling the corresponding free() for for a new operator calling the corresponding delete operator.
locka
A further thought, if you're using C++ you can use "std::cout << *data[3]" as an alternative way to output an int. The benefit is if you change the type of your array (e.g. to a long) that you don't have to change the output because C++ streams have overloads to support every primitive.
locka
+2  A: 

You should place the printf statement in the loop:

k[0]=2; 
k[1]=3; 
k[2]=4; 

for (i = 0 ; i <= 2 ; i++) 
{ 
    x[i]=5*k[i]; 
    printf("%d ", x[i]);
} 

printf("\n");
Grimmy
A: 
#include <stdio.h>
#define FOR_PRINT(index, array, size, format) \
    { \
        for(index = 0; index < size; ++index){ \
            printf(format, array[index]); \
        } \
        printf("\n");
    } \

int main(){
    int array [] = {1,2,3};
    int i;
    FOR_PRINT(i, array, 3, "%d ");
    return (0);
}

You can always use an MACRO for that.

My previous example is not the safest, or the most readable example of a macro, but can be a starting point for you.

Andrei Ciobanu
Hmm, what is the reason for doing this with a macro?
Kinopiko
-1: Always avoid macros whenever possible. This case absolutely does not require a macro.
DeadMG
The old mantra, "never use macros 'if you don't need them'".
Andrei Ciobanu
+1  A: 

You have stored 100 numbers in an array and want to display the same.

In simple language, we can say an array is collection of items of similar types.

For this, you have to first declare the array size, suppose you have declared an array as x[100], then it will store 100 numbers, with index number 0 to 99 or you can do the same by declaring each variable like x[0]=20,x[1]=30 .......x[99]=98, but this will be more time consuming.So its better if you do as follows.

Accept values

int x[100];    
 int index;
 for(index=0;index<100;index++)
 {
        printf("\nEnter %d number:",index+1);
        scanf("%d",x[i]);
  }

Display

for(index=0;index<100;index++)

{

printf("\nArray element at index %d is %d",index,x[i];

}

This will be help you.

Vibhakar SInha