Thread: static array list

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

    static array list

    I'm trying to make static list that can add element in list. I know that's know the good way to make list but it's just experiment

    only 5 digits allow in list

    Program supposed to give result like this

    add digit 1 in array then show array = 1
    add digit 2 in array then show array = 1 2
    add digit 3 in array then show array = 1 2 3
    add digit 4 in array then show array = 1 2 3 4
    add digit 5 in array then show array = 1 2 3 4 5


    When array exceed limit size, stop adding digit

    Code:
    #include<stdio.h>
    
    
    int main ()
    {
    	int i;
    	
    	int array[5];
    	
    	return 0;
    }
    I need a function that add one digit in array and show the array digits

    I'm stuck with function I don't understand what's logic need to add one digit

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    146
    To add first digit => array[0] = 1
    To add second digit => array[1] = 2
    and so on until 5th digit which will be in array[4]

    To print the array use a simple for loop.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    81
    Quote Originally Posted by thmm View Post
    To add first digit => array[0] = 1
    To add second digit => array[1] = 2
    and so on until 5th digit which will be in array[4]

    To print the array use a simple for loop.
    I can do that but I am looking for a function

    Code:
    #include<stdio.h>
    
    int main ()
    {
    	int i;
    	
    	int array[5] = {1};
    	
    	for ( i = 0; i <5; i++)
    		printf(" %d", array[i]);
    	    
    		array[1] = 2;
    	
        printf("\n");
    	
    	for ( i = 0; i <5; i++)
    		printf(" %d", array[i]);
    	
    		array[2] = 3;
    	
        printf("\n");
    	
    	for ( i = 0; i <5; i++)
    		printf(" %d", array[i]);
    	
    	    array[3] = 4;
    	
        printf("\n");
    	
    	for ( i = 0; i <5; i++)
    		printf(" %d", array[i]);
    	
    	
           array[4] = 5;
    	
        printf("\n");
    	
    	for ( i = 0; i <5; i++)
    		printf(" %d", array[i]);
    	
    	return 0;
    }
    1 0 0 0 0
    1 2 0 0 0
    1 2 3 0 0
    1 2 3 4 0
    1 2 3 4 5

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,410
    Maybe:
    Code:
    #include <stdio.h>
     
    void push(int *a, int *size, int value) {
        a[(*size)++] = value;
    }
     
    void print(const int *a, int size) {
        while (size-- > 0) printf("%d ", *a++);
        putchar('\n');
    }
     
    #define CAPACITY 5
     
    int main() {
        int a[CAPACITY], size = 0;
        for (int i = 1; i <= CAPACITY; ++i) {
            push(a, &size, i);
            print(a, size);
        }
        return 0;
    }
    A long time ago being crazy meant something. - Charles Manson

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    81
    Quote Originally Posted by john.c View Post
    Maybe:
    How do you do if number is not in sequence ?

    2
    2 9
    2 9 3
    2 9 3 6
    2 9 3 6 5

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,410
    Push whatever values you want. I just used a counting loop.
    A long time ago being crazy meant something. - Charles Manson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with static linked list
    By ricardo904643 in forum C Programming
    Replies: 4
    Last Post: 01-07-2014, 12:00 AM
  2. why can't a static array copy values from a dynamic array
    By c++noob145 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2013, 10:25 AM
  3. Changing static array database system to linked list
    By DLH112 in forum C Programming
    Replies: 13
    Last Post: 12-09-2009, 12:50 PM
  4. Static library how do i list
    By gracy.f in forum Linux Programming
    Replies: 2
    Last Post: 10-20-2005, 10:58 AM
  5. Replies: 5
    Last Post: 04-09-2004, 10:13 AM

Tags for this Thread