-2

I have tried making a dynamic array in C but I wont make it work so I'm trying to use a normal array where I just delete the rest numbers that are getting printed out. I have a array:

int array[100];

this array will store 100 numbers, annd these numbers will be scanned in with a for loop. In that for loop I do have a if statement:

if(array[i] == 0)
{
    break;
}

so if I scan the number 0 the for loop will break and the code will continue. When I scan normal numbers I want for example to scan 20 numbers. The array has 100 spot for numbers but I only write 1-20 and then I type 0 which makes the loop break. Then I do have a for loop for printing the array. The printf prints the whole array 1-20 and after the 20 it will start to print out the rest 80 numbers that haven't got a number assigned. So my question is how am I able to remove all these rest numbers that gets printed out after my 1-20 numbers that I ACTUALLY scanned in? I just want the scanned numbers to be shown not all the 100.

4 Answers4

2

Use this:

i=0;
while(array[i]){
    printf("%d\n",array[i++]);
}

EDIT:
After the comments and all, I thought I'd write a simple code for you to use in your scenario. I have commented the code. It should help you understand everything. If you still have doubts, feel free.

#include<stdio.h>

void sortera(int array[], int n, int m)
{
    int tmp,i,j;
    for(i=n; i<m; i++)
    {
        for(j=n;j<m; j++)
        {
            if(array[j]>array[j+1])
            {
                tmp=array[j];
                array[j]=array[j+1];
                array[j+1]=tmp;
            }
        }
    }
}

int main(){
    int i,c;
    int a[100];
    c=0;        //this c keeps the count
    for(i=0;i<100;i++){     //since 100 is the max limit
        scanf("%d",&a[i]);
        c++;                //this is what @JackWilliams meant you to do
        if(a[i]==0)
            break;
    }
    printf("\nInput array\n");
    i=0;
    while(a[i])
        printf("%d\n",a[i++]);      //this is my snippet that prints all values
    //now sorting. I changed the algo a bit that you wrote (basic idea is same though)
    //This code sorts the array elements in range n..m, inclusive of n and xclusive of m
    //Since arrays in c are (by default) passed by reference, you just need to sort them. No return value is required
    sortera(a,0,c);
    //Now I use @JackWilliam's method to print the array
    printf("\nSorted Array:\n");
    for(i=0;i<c;i++)
        printf("%d\n",a[i]);
    return 0;
}
vish4071
  • 5,135
  • 4
  • 35
  • 65
  • Hmm, this works and prints the array for me exacly like I want it, but I do have a function that sorts all the numbers for me so I have the function; sort(array, i, j); but when I added your code it doesn't print out the sorted numbers it prints out the unsorted numbers. – andrewjones Sep 17 '15 at 18:35
  • My code just prints the array from starting upto the point where array[i] is `0`. It then does not print anything. If your sort function is sorting the array correctly, from `i..j`, where value of `array[j+1]` should be `0`, this line *will* print the array correctly. – vish4071 Sep 17 '15 at 18:40
  • It may be that your sorting function is buggy. If you post the entire code, I think I can help but otherwise its not possible. – vish4071 Sep 17 '15 at 18:41
  • I tried to do what you said, it sorts all the numbers from 1-10 but not >10 I don't understand why. Heres the code for the function: http://pastebin.com/77sZgSCD – andrewjones Sep 17 '15 at 18:47
  • You are sorting 10 elements there. In your loop, just replace `10-1` with `20-1` (for 20 elements). It should work then. In any case, if you need a general case, you need a count of how many elements you are actually inputting. – vish4071 Sep 17 '15 at 18:49
  • Hmm, still it doesn't sort inputs higher than 10, if I input 55 it just leaves 55 and ruins the output after it. – andrewjones Sep 17 '15 at 18:54
  • There you go. @sameizad – vish4071 Sep 17 '15 at 19:09
  • sortera(a,0,c); would you mind explaining all the variables? I dont understand the 0. – andrewjones Sep 17 '15 at 19:30
  • `sortera` takes in 3 parameters, 1st is the array reference. Other 2 are limits of the array you want to sort. (Say, if you want to sort array from 4th to 10th index (inclusive), and leave all other elements in their position, you will call `sortera(a,4,11)` ). Since I wanted to sort whole array, I passed `0` and `c`. – vish4071 Sep 17 '15 at 19:34
1

From your description I would suppose you just have to store the last i (from array[i]) and print the array for all elements until this last number. You can choose if you want the tailing zero or not by running the loop to < or <= this latest index.

JeffRSon
  • 10,404
  • 4
  • 26
  • 51
  • How would I be able to find the last i when I have 100 spots and just scan in 20 ? How would I for example find the 20 i? Lets say we now know that I will be scanning in 20 ints but later I wont be able to know how many ints the user will be writing in the input. – andrewjones Sep 17 '15 at 18:25
  • The if-condition that breaks the loop should have the last `i`. – JeffRSon Sep 17 '15 at 18:54
0

Why not keep a knt variable and increment it each time you scan a number and then you will know how big the array is. So to print it you could do:

for(int a = 0; a < knt; ++a) {
    printf("%d", array[a]);
}
TheQAGuy
  • 498
  • 7
  • 17
  • so I should make knt = 1; first time and then in the for loop I would do knt++; right? – andrewjones Sep 17 '15 at 18:22
  • @sameizad No start with `knt = 0` because you declare knt before you scan anything. @vish4071 has an easier solution though so i would use his. – TheQAGuy Sep 17 '15 at 18:24
  • Yeah, but before all this I tried to do what u just wrote. if I start with knt = 0 the loop wont start because a !< knt if it's 0. So I have to start with knt = 1 and when I scan in and add knt++; when I have scanned in 13 times the program crashes.. – andrewjones Sep 17 '15 at 18:31
  • @sameizad You need two seperate loops first scan in the numbers and increment knt each time and then when your done scanning the numbers knt will be 20 so you make a new loop as i stated above – TheQAGuy Sep 17 '15 at 18:55
0

If you know (or can know) what is the size of your array (not the size that was created, but how many elements are being used), then create a variable to it, and instead of comparing with zero, checks if the variable 'i 'reached this index.

Here is a good discussion of dynamic arrays: How to declare a dynamic integer array in ANSI - C using malloc and put input integers into it?

Here, using a more elaborate structure: Explanation of code (linked list C)

Community
  • 1
  • 1
Paulo Pinheiro
  • 118
  • 1
  • 4