0

How to find how many elements are filled in an array

Eg There is Ann array of size 50 . I has 5/ more elements in it which i don't know. But i want to write such code which will help me to find how many elements are there in an array. For better understanding in python we can easily find by using length function. But in c how to find it . How to find array is empty and '/0' doesn't to check. Eg arr[50]; No value inserted yet. Checking array is empty or not : arr[0]=='/0' this condition is showing error and not working.

13th Nov 2022, 11:08 AM
Om Yele
Om Yele - avatar
13 Answers
+ 3
You already asked this question today and got a response. https://www.sololearn.com/Discuss/3103628/?ref=app In C it makes no sense to check, if an array element is filled. When you declare the array, the memory that is allocated to it , may already contain some leftover garbage values, so there is no way to tell if those were there initially or you put them there. You either keep track in a separate variable, how many values you have used, or just make sure in your algorithm that you never try to read a value which you have not put there. C and Python are completely different, Python has too much convenience and magic, which you have to let go, because C is very rough and bare bones.
13th Nov 2022, 11:29 AM
Tibor Santa
Tibor Santa - avatar
+ 3
No such built-in function exists. You have to build it yourself. But feel free to browse the complete C standard library, it's not that big. https://en.cppreference.com/w/c/header
13th Nov 2022, 1:04 PM
Tibor Santa
Tibor Santa - avatar
+ 2
And it was said that you can't do this easily in C. Several workarounds have been suggested to you. Maybe tell some more detail about your project, what is it what you're trying to achieve, even attach your code. You will more likely to get meaningful suggestions that way.
13th Nov 2022, 11:39 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Save your code in sololearn code playground. Insert a link in forum comment. https://www.sololearn.com/post/1713793/?ref=app
13th Nov 2022, 12:18 PM
Tibor Santa
Tibor Santa - avatar
+ 2
So you already have a top variable in your code. I think the purpose is to store how many elements of the array are currently used.
13th Nov 2022, 12:46 PM
Tibor Santa
Tibor Santa - avatar
13th Nov 2022, 12:22 PM
Om Yele
Om Yele - avatar
+ 1
Correct . See what I am trying to do is in insertatspecpos function there is printed that You cannot insert at any position but you can from 0 to %d , arr[fixedtop-1]. In this it retiring the value but i want it's index no . Just now while texting it got strike in my mind that to find its index no i can write function which will first return arr[fixedtop-1] value and i will compare it with all other value . where it will find then counter will return its value as an index no . But i want a function which is builtin which will do my work easily.
13th Nov 2022, 12:51 PM
Om Yele
Om Yele - avatar
0
That what I am asking how to do in c . I am working on project
13th Nov 2022, 11:36 AM
Om Yele
Om Yele - avatar
0
#include <stdio.h> #include <conio.h> #include <stdlib.h> #define size 50 int arr[size], choice, top = 0, position, insert, fixedtop, i, j = 1; int insertatspecpos(); // Inserting value at specific position int traversearr(); // Printing all the values of an array int deletearr(); // not completed int insertatFpos(); // Inserting value at last position int insertatLpos(); // Inserting value at first position int main() { while (1) { printf("\n"); printf("\n 1) Insert element in an array \n 2) Insert at specific location \n 3) Traversal of an array \n 4) Insert at first location \n 5) Deletion of an array \n 6) Exit from a program"); printf("\n\nSelect the no to perform an operations: \n"); scanf("%d", &choice); if (choice == 1) { insertatLpos(); } else if (choice == 2) { insertatspecpos(); } else if (choice == 3) { traversearr(); } else
13th Nov 2022, 12:12 PM
Om Yele
Om Yele - avatar
0
#include <stdio.h> #include <conio.h> #include <stdlib.h> #define size 50 int arr[size], choice, top = 0, position, insert, fixedtop, i, j = 1; int insertatspecpos(); // Inserting value at specific position int traversearr(); // Printing all the values of an array int deletearr(); // not completed int insertatFpos(); // Inserting value at last position int insertatLpos(); // Inserting value at first position int main() { while (1) { printf("\n"); printf("\n 1) Insert element in an array \n 2) Insert at specific location \n 3) Traversal of an array \n 4) Insert at first location \n 5) Deletion of an array \n 6) Exit from a program"); printf("\n\nSelect the no to perform an operations: \n"); scanf("%d", &choice); if (choice == 1) { insertatLpos(); } else if (choice == 2) { insertatspecpos(); } else if (c { traversearr(); } else
13th Nov 2022, 12:13 PM
Om Yele
Om Yele - avatar
0
Here unable to put entire code. How to put code
13th Nov 2022, 12:14 PM
Om Yele
Om Yele - avatar
0
If it is empty or you can say as a null character then it automatically allocate garbage value to that null character.
13th Nov 2022, 6:04 PM
Raunak Tiwari
0
How to represent that garbage value . Garbage value can be which will be not equal an integer . Then how to compare it . Provide the code
14th Nov 2022, 2:33 AM
Om Yele
Om Yele - avatar