Thread: help me with array

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Indonesia
    Posts
    8

    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...










    Last edited by icor15; 11-24-2012 at 07:15 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Indonesia
    Posts
    8
    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
    Last edited by icor15; 11-24-2012 at 08:37 AM.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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. #5
    Registered User
    Join Date
    Nov 2012
    Location
    Indonesia
    Posts
    8
    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. #6
    Registered User
    Join Date
    Nov 2012
    Location
    Indonesia
    Posts
    8
    please can you explain the number 4??
    I did number 3 and right,.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Location
    Indonesia
    Posts
    8
    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)
    Last edited by icor15; 11-24-2012 at 10:19 AM.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    32
    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..
    Last edited by amzc; 11-24-2012 at 10:19 AM.

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    32
    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. #10
    Registered User
    Join Date
    Nov 2012
    Location
    Indonesia
    Posts
    8
    thank you amzc,, it help me and I'm going to submit my assignment in a while,

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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)
    instead of
    Code:
    main()
    that you had.
    Or shouldn't I? Have you been taught about functions?


    Also both read about Indent_style

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    32
    @std10093 : Ok... I am sorry for this time. I wont repeat like that again ok.... I am also a student so I helped like this because icor15 mention he has only 4 hrs left to submit his assignment that only I do like this.

    @icor15 : try to learn extra when you have free time..

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by amzc View Post
    @std10093 : Ok... I am sorry for this time. I wont repeat like that again ok.... I am also a student so I helped like this because icor15 mention he has only 4 hrs left to submit his assignment that only I do like this.
    No problem. It seems that you understand what I am saying. Also I fully understand what you are saying about the fact that icor15 had to submit

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 09-09-2012, 02:29 PM
  2. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  3. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  4. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM

Tags for this Thread