Thread: Very easy task with arrays, but a cannot do it

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    4

    Very easy task with arrays, but a cannot do it

    It's a very easy task, but for some reason I cannot do it, I'm trying to do it for many hours already, help me please.

    Make a function that has as its parameters length of 2 arrays and these 2 arrays too. It needs to assign all nonzero values from 1st array to 2nd, from left to right in ascending order and return number of significant ( that were assigned to 1st to 2nd ) elements of 2nd array.
    Prototype:
    Code:
    size_t counter(size_t length, int input[length], int output[length]);
    If it was not understandable I'll give an example.
    Code:
    input {1, 4, 0, -6, 0} --> output {-6, 1, 4, trash, trash}
    return 3

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    814
    Show us the code you have tried already, for instance, you probably went with a loop of some kind.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    4
    The problem that, is I'm stuck on assigning elements from 1st do 2nd, I just cannot figure out how to do it normally, without very long and hard methods

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,497
    > It needs to assign all nonzero values from 1st array to 2nd, from left to right in ascending order and
    > return number of significant ( that were assigned to 1st to 2nd ) elements of 2nd array.
    So ignore "ascending order" for the moment, and see if you can get as far as say
    Code:
    input {1, 4, 0, -6, 0} --> output {1, 4, -6 }
    return 3
    Learning how to break down problems into more manageable steps is just as valuable skill as knowing where the curly braces go.

    Perhaps more important; if you can only solve problems which are immediately obvious and coded in a single keyboard session, you're not going far.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    4
    Quote Originally Posted by Salem View Post
    >
    So ignore "ascending order" for the moment, and see if you can get as far as say
    I already tried to copy elements from 1st to 2nd, and succesfully done it, but because of trash elements i am not able to do normal sorting

  6. #6
    Registered User
    Join Date
    Oct 2019
    Posts
    4
    I've just finally managed to do it, solving was so close that I needed to add less than 15 symbols, thanks everyone.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy question about arrays
    By shotakobakhidze in forum C Programming
    Replies: 10
    Last Post: 09-21-2015, 11:49 AM
  2. Help with arrays task
    By LMoroney in forum C Programming
    Replies: 3
    Last Post: 11-30-2014, 09:50 AM
  3. Replies: 2
    Last Post: 12-31-2007, 11:40 AM
  4. Probrem With Switch (Another Easy Task):
    By Maragato in forum C Programming
    Replies: 3
    Last Post: 07-25-2004, 05:29 AM
  5. 2D arrays with functions made easy
    By goran in forum C Programming
    Replies: 1
    Last Post: 09-17-2001, 12:08 PM

Tags for this Thread