Can we use array.length when we dont know lengt of array.I use C language
17 3993
Can we use array.length when we dont know lengt of array.I use C language
You can apply something like following to find the length of an array -
if array has three values like this:
-
int var[10]={1,2,3};
-
for(int i=0;i>=0;i++)
-
{
-
if(var[i]!=NULL)
-
{
-
len++; this will be the length of array
-
}
-
else break;
-
}
-
Regards
Erm, you can't find the length of an array unless the last element of the array is some sentinel value.
You can apply something like following to find the length of an array -
if array has three values like this:
-
int var[10]={1,2,3};
-
for(int i=0;i>=0;i++)
-
{
-
if(var[i]!=NULL)
-
{
-
len++; this will be the length of array
-
}
-
else break;
-
}
-
Regards
thanks your information.But i wanna use a array in for loop.But I dont know the length of array.So can I use that kind of code for that.
for(int i;i<array.length;i++)
{
...
}
thanks
mihiri
Erm, you can't find the length of an array unless the last element of the array is some sentinel value.
Yes, number of elements can be found but not length "unless the last element of the array is some sentinel value". :))
To use array length you can use some dyanamic array and give last value to @ or some other such value and count until that value arrives.
Yes, number of elements can be found but not length "unless the last element of the array is some sentinel value". :))
To use array length you can use some dyanamic array and give last value to @ or some other such value and count until that value arrives.
thanks your reply .I ve another problem.Can you pls help me.can I increment array using same parameter in array.I mean,
#include <stdio.h>
int main()
{
int k;
int array [k];
for(k=0;k<array.length;k++)
..................
can I make a for loop like this
thanks your reply .I ve another problem.Can you pls help me.can I increment array using same parameter in array.I mean,
#include <stdio.h>
int main()
{
int k;
int array [k];
for(k=0;k<array.length;k++)
..................
can I make a for loop like this
That wouldn't be correct. Use another variable for the index like i and replace array.length with k
thanks your reply .I ve another problem.Can you pls help me.can I increment array using same parameter in array.I mean,
#include <stdio.h>
int main()
{
int k;
int array [k];
for(k=0;k<array.length;k++)
..................
can I make a for loop like this
First of all, array.length is not possible. You can increment array using dynamic array like this
int *array = new int[];
and then delete it after use.
delete [] array;
What you have wriiten is fixed size array :))
First of all, array.length is not possible. You can increment array using dynamic array like this
int *array = new int[];
and then delete it after use.
delete [] array;
What you have wriiten is fixed size array :))
this is not a fixed array.So how can I go to end of the array and I use C language
this is not a fixed array.So how can I go to end of the array and I use C language
For that r035198x has given the solution. If you do not understand here is the code.
(referred frm ur example)
int k,
int array[k];
for(int i=0; i<k;i++)
{
{Do whatever}
}
Cheers!! :))
For that r035198x has given the solution. If you do not understand here is the code.
(referred frm ur example)
int k,
int array[k];
for(int i=0; i<k;i++)
{
{Do whatever}
Cheers!! :))
}
thank you very much ur help .but I wanna to increment array.ie acording to ur code I wanna increment k.that mean I wanna travel the array.and also i dont know array size.thats my prob(I dont wanna increment other variable as i)
}
thank you very much ur help .but I wanna to increment array.ie acording to ur code I wanna increment k.that mean I wanna travel the array.and also i dont know array size.thats my prob(I dont wanna increment other variable as i)
You can not increament fixed size array. One you have given the value 10 for that variable than you cannot programmatically increment it to 15. Only dynamic array can do that. They will give u facility to add whatever number of values u need.
If possible give ur problem in detail, what your program want to do.
Regards
You can not increament fixed size array. One you have given the value 10 for that variable than you cannot programmatically increment it to 15. Only dynamic array can do that. They will give u facility to add whatever number of values u need.
If possible give ur problem in detail, what your program want to do.
Regards
there is a fnction and its return values i stored to a array.So this array is not fixed.I have ti travel this array element by element.So I use for loop for travel the array.So i define array like int k;int array[K];and use for(k;k<end of array;k++)
*This is not a fixed array
*I wanna to travrel the array until last element
*I dont kno array size
this is my problem
there is a fnction and its return values i stored to a array.So this array is not fixed.I have ti travel this array element by element.So I use for loop for travel the array.So i define array like int k;int array[K];and use for(k;k<end of array;k++)
*This is not a fixed array
*I wanna to travrel the array until last element
*I dont kno array size
this is my problem
OK.
So you can define a dynamic array like -
int *array = new int[];
-
int count = 0;
-
-
for (int i = 0; i <whatever time you want to insert the new element; i++)
-
{
-
cin>>value; //scanf incase of C
-
array[count] = value; //This will add to array after last element
-
count++;
-
}
-
Finally u will get total number of elements in array in count. But each time automatically last element will be added dynamically by this method.
If you still have some doubt then search dynamic arrays in C in google :))
Regards
OK.
So you can define a dynamic array like -
int *array = new int[];
-
int count = 0;
-
-
for (int i = 0; i <whatever time you want to insert the new element; i++)
-
{
-
cin>>value; //scanf incase of C
-
array[count] = value; //This will add to array after last element
-
count++;
-
}
-
Finally u will get total number of elements in array in count. But each time automatically last element will be added dynamically by this method.
If you still have some doubt then search dynamic arrays in C in google :))
Regards
Is there any alternative to this in C++ ?
Regards,
Girish.
Banfa 9,065
Expert Mod 8TB
Is there any alternative to this in C++ ?
Use a vector
You can only find the length of an array when you can refer to that array *not* in
a value context, i.e. the array name is *not* passed as a parameter, nor is it
dynamically allocated; only *then* you can do this: -
#define LENGTH(a) (sizeof(a)/sizeof(*a))
-
...
-
-
int a[42];
-
int l= LENGTH(a); // l == 42;
-
kind regards,
Jos
Banfa 9,065
Expert Mod 8TB -
int *array = new int[];
-
int count = 0;
-
-
for (int i = 0; i <whatever time you want to insert the new element; i++)
-
{
-
cin>>value; //scanf incase of C
-
array[count] = value; //This will add to array after last element
-
count++;
-
}
-
This results in corrupted memory on my computer (using MSVC 6.0)
C99 supports dynamic arrays, but this isn't C it's C++ and C++ doesn't since it has the vector to deal with that problem.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: chennakeshava_ramesh |
last post by:
hi,
I have a problem, I am not able to find out which day of the week it
is using the calendar class.
I am using set() function to set the date
and want to find out which day i.e mon,tue etc of...
|
by: Greg Baker |
last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to
post code and ask for help? I hope so.. :)
Here's my problem: I am trying problem 127 of the valladolid online...
|
by: Bob |
last post by:
Can anyone make this legible (format?) and tell me if there's any
error or something that calls a pop-up "message dialog box" ?
Cheers...
q18=0;q19=new Array();q61...
|
by: Mike Bartels |
last post by:
Hi Everyone!
I have two Arrays A and B. Both arrays are byte arrays with 7 bytes each.
The contents of array A and B are the same
A = {1, 2, 3, 4, 5, 6, 7};
B = {1, 2, 3, 4, 5, 6, 7};
When...
|
by: vmoreau |
last post by:
I have a text and I need to find a Word that are not enclosed in
paranthesis. Can it be done with a regex? Is someone could help me?
I am not familar with regex...
Example looking for WORD:...
|
by: =?Utf-8?B?VGhvbWFzVDIy?= |
last post by:
Hello,
I have an array that holds 4 values ( they contain random numbers). I would
like to find the lowest value (if there is a tie i would like to find one of
the tie.) then remove that value....
|
by: emily224 |
last post by:
Hello,
I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
|
by: emily224 |
last post by:
Hello,
I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
|
by: C C++ C++ |
last post by:
Hi all, got this interview question please respond.
How can you quickly find the number of elements stored in a a) static
array b) dynamic array ?
Rgrds
MA
|
by: mouac01 |
last post by:
Newbie here. How do I do a find and replace in a binary file? I need
to read in a binary file then replace a string "ABC" with another
string "XYZ" then write to a new file. Find string is the...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |