Thread: help me with array

1. help me with array

I'm icor15, from Indonesia. Nice to meet you all, I will learn here (so many knowledge I can find here)
I'm very beginner with C language and I have my assignment for Computer Science
can anyone help me
array one dimension with
Code:
```#include<stdio.h>
main(){
int A[15]={11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
...```

to make 1)
11 1213 14 15
16 1718 19 20
21 2223 24 25

2)
11 1213
14 1516
17 1819
20 2122
23 2425

3)
11
12 13
14 1516
17 1819 20
21 2223 24 25

4)
111213 14 15
16 17 18 19
20 21 22
23 24
25

5)
11
1213
14 1516
17 1819 20
21 2223 24 25
17 1819 20
14 1516
12 13
11

I have 4 hours left before I must submit this assignment , I have no idea
I will be very thankful if you can help me...

2. Hi, my father has been there for some aviation business and he liked it.Also welcome to the forum!

Ok so you have your array.If i understood the task, you should print out in the screen this numbers (output).

Are you aware of printf function? Also are you aware of the newline character?

Example
Code:
```int a = 5;
int b = 10;
int c = 90;

printf("%d %d %d",a,b,c);```
This will output
Code:
`5 10 90`

Now i will use the newline character \n to get 90 in a new line.
Code:
```int a = 5;
int b = 10;
int c = 90;

printf("%d %d \n %d",a,b,c);```
Output:
Code:
```5 10
90```
Ok now about the array.If i want to access the first element of the array i have to write it like this
Code:
`array[0];`
If i want to access the i-th element of the array I write this

Code:
`array[i];`
So how to print the 4th element of the array?
With this code
Code:
`printf("%d",array[3]);`
So how to print the i-th element of the array?
With this code
Code:
`printf("%d",array[i]);`
Ok, how to print all the elements of the array?
Code:
```#include <stdio.h>

int main(void)
{
int array[] = {0,1,2,3,4,5,6,7,8,9};
int i;

/* Traverse trough the array */
for(i = 0 ; i < 10 ; i++)
{
/* Print the current element and leave a space */
printf("%d " , array[i]);
}

/* Just leave a line */
printf("\n");

return 0;
}```
Output
Code:
```Macintosh-c8bcc88e5669-9:~ usi\$ ./px
0 1 2 3 4 5 6 7 8 9
Macintosh-c8bcc88e5669-9:~ usi\$```
Now , every two elements i print i will change line.An easy way of doing this is to have an extra variable named counter.I will increment by one in every loop iteration and when the counter is equal to two, then i will print a newline (change line) and then I will reset the counter to zero.
Code:
```#include <stdio.h>

int main(void)
{
int array[] = {0,1,2,3,4,5,6,7,8,9};
int i,counter;

/* Traverse trough the array */
counter = 0;
for(i = 0 ; i < 10 ; i++)
{
/* Print the current element and leave a space */
printf("%d " , array[i]);

counter++;
if(counter == 2)
{
printf("\n");
counter = 0;
}
}

/* Just leave a line */
printf("\n");

return 0;
}```
Output:
Code:
```Macintosh-c8bcc88e5669-9:~ usi\$ ./px
0 1
2 3
4 5
6 7
8 9

Macintosh-c8bcc88e5669-9:~ usi\$```
Now try to apply these on your homework...Or get inspired by this and find a way of achieving the results you want.
If trouble comes up, post back

Hope this helps.

3. thank you so much for the help std10093
I can do number 1 and 2 but I still stuck after I face number 3
and I still have no idea for number 4 and 5
how to do it? glad I joined and posted here, I got knowledges from amazing and kind members

4. You are welcome

What did you try for the number 3 and got stuck? Post your code and wrap it with code tags like this

[key]/*YOUR CODE HERE*/ [/key] Replace the word key with the word code in order this to work.

5. Code:
```int main(void){
int A[15]={11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
int i,j;
j=0;
for (i=0 ;i<15;i++)
{
printf("%d ",A[i]);
j++;
if(j==1)
{
printf("\n");
j = 0;
}
else if(j==2)
{
printf("\n");
j=0;
}
else if(j==3)
{
printf("\n");
j=0;
}
else if(j==4)
{
printf("\n");
j=0;
}
}
printf("\n");
return 0;

}```

i don't know if I did it right......

6. please can you explain the number 4??
I did number 3 and right,.

7. 11 12 13 14 15
- - 16 17 18 19
------ 20 21 22
---------23 24
------------25
seems I type wrong in the question,, how to do it??
(------ represent space)

8. Code:
```#include <stdio.h>

void main()
{
int a[] = {11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
int j,i,count=0;
for(i=1;i<6;i++)
{
for(j=0;j<i;j++)
{
if(j+1==3)
printf("%d",a[count]);
else if(j+1!=3)
printf(" %d",a[count]);
}

printf("\n");
}
}```
I thing it will help you for the 5th one ..but you have to increment the count value and repeat the given code with decrement the i value....
it will help means rate me..

9. the 4th one is very simple..
Code:
```for(i=5;i>=0;i--)
{
sp=i;
for(;sp<=5;sp++)
printf(" ");
for(ar=i;ar>=0;ar--)
{
printf("%d",array[count]);
}
}```
you got it...

10. thank you amzc,, it help me and I'm going to submit my assignment in a while,

11. Hey I had to go out...

copy pasting just code won't help at all.I mean it will save the assignment once but what will happen when the next comes up? You should learn from every single line of code you write...
You should understand why you write a line of code like this and not the other way....

@amzc I understand that you want to help but you did not actually help like this. It is like letting your classmate copy your assignment .He will submit but he will not learn!
You should provide explanation....Why the loop has this conditions for example and everything else that have to be explained.I am telling you that in a friendly way because I was in your shoes in my first posts
- in my first posts as helper, because i joined the forum in first place to get helped - and some members pointed that out to me (believe me not as kind as I am speaking now).But they had right.

@icor15 -> Are you sure you understood the code?Do you have any questions?
Also now that the submission is done I would like to explain why we return 0 from main and why we write
Code:
`int main(void)`
Code:
`main()`
Or shouldn't I? Have you been taught about functions?