Tuesday, July 14, 2009

How do i add values to an array in C++ using a function.?

I have a set of values that i read into an array using a function earlier on, now i need to add additional values to the end of the array. hot do i go about doing that.

How do i add values to an array in C++ using a function.?
probably u had used a loop for earlier reading ,


then here's how it goes . i'll show an example.


#include%26lt;iostream.h%26gt;


void main()


{int a[20],i,n,m;


cout%26lt;%26lt;"\n How many elements ?";


cin%26gt;%26gt;n;


for(i=0;i%26lt;n;i++)


{


cin%26gt;%26gt;a[i];


}


cout%26lt;%26lt;"\n How many elements do you want to add ?";


cin%26gt;%26gt;m;


for(;i%26lt;n+m;i++)


cin%26gt;%26gt;a[i];


}





the idea here is , u hav executed for loop for the first reading , so u dont change the value of i that ws updated when it ws in the loop. Further reading takes place frm wer it ws stopped. Bt ur array size should be such that it can accomodate m+n elements
Reply:These are some way you can do


1. new array with maximize size array[MAX] and int _array_len variable.


Ex: len = 2;


buffer = new[len];


//TODO: init value


when u want to add value then just increase len++


buffer[len++] = value;


2. You should use collection like List or ArrayList





I hope it can help


How to read the length of an array in c language?

i need help with reading the length of an array


i'm really bad with arrays, i'm a beginner at this


ex.


int a[] = {7, 5, 5, 15, 46, 12};





please i'm suffering!!!!!!!!!!!!!!!!!!!!!...

How to read the length of an array in c language?
int arrayLen = (sizeof a)/(sizeof int);
Reply:are you asking how many elements are in that array or for a function to give you the amount of elements in an array?
Reply:Your question is a very good one, since it highlights a big deficiency in the C language. There is no way in C to programmatic determine the length of an array. The person programming this code must use their eye balls to count the integers in the array. In this case the array contains 6 elements. A much safer way to declare this array would be:





#define SIZE 6





int a[SIZE] = {7, 5, 5, 15, 46, 12 };





You can then use the SIZE constant when looping over the array. If you were to add a 7th or 8th element into the array, but forgot to update the SIZE constant you would get a compile time error, which is a good thing, since it is always better to find problems at compile time than at runtime.





If you were using the Java language determining the length of an array is much simpler. Java has a handy length property for all arrays. In your case you would just code "a.length" to determine the length of the array.

carnation

Storing a name in array using C?

When the program starts I need to prompt for the Users name and store it in an array . I need to be able to frequently insert it in the printf("Ben (this was entered earlier) , please enter blah blah...") like so. Can someone please help me with this. I appreciate any help , or even a link where I can look it up as I was not able to find much on this . Thank you :)

Storing a name in array using C?
There are two ways, select which you feel suitable


first


char *name; // no matter if name istoo long


printf("Enter your name:");


scanf("%s",name); //or you can use


//gets(name);


//to print simply


printf("%s",name);





or by using arrays


char name[20]; //length limited upto 20 chars only


printf("Enter name:");


scanf("%s",name);


printf("%s",name);
Reply:http://www.scit.wlv.ac.uk/cbook/chap6.ar...





#
Reply:I don't get a full picture of what you want out of your question. I presume you are trying to deal with some kind of array of structured objects.


This site might guide you through..


http://vergil.chemistry.gatech.edu/resou...


What is program code in turbo c using array?

you must enter 3 name of a students with 3 grade in each student and the output are will be this





first table will be in alphabetical order


and the second table will be in highest to lowest order


for example:





calbin 90 91 90 ave is 90%


justin 80 80 80 ave is 80%


mark 85 85 85 ave is 85%





calbin 90 91 90 ave is 90%


mark 85 85 85 ave is 85%


justin 80 80 80 ave is 80%











please HELP me!!!!! SOS

What is program code in turbo c using array?
What do you need help with??? Try to program it yourself and when you run into problems, post here.


What is the max array size of Array in C?

ie int array[MAX]... What is the maximum value i can assign to MAX ?

What is the max array size of Array in C?
http://www.thescripts.com/forum/thread21...





You can determine a guaranteed minimum if you know sizeof int. The actual limit depends on the implementation.
Reply:32,768 Usually- depends on the compiler you are using.
Reply:some around 35000, depends on the compiler you are using.


Filling a 2d array in C++?

how do u fill a 2D array with 6 names where each name can have a maximum of 24 characters long. ( preferred done in a function).

Filling a 2d array in C++?
function fillArray()


{


char names[6][24];


string temp=" ";


int count=0;


count %26lt;%26lt; "Enter the intial name";


cin %26gt;%26gt; temp;





while(temp!="END")


{


for(int i=0;i%26lt;temp.length;i++)


{


names[count][i] = temp[i];


}


count++;


count %26lt;%26lt; "Enter name, enter END to stop";


cin %26gt;%26gt; temp;


}











}

pansy

Declaring an array in C++?

Two (related) questions:





What's the difference between





public int[ ] sourceType;





in a header file, and





int sourceType[ ];





in the body of a program?





**** **** **** **** **** **** ****


The program I have is giving me an index out of bounds error when iSource (below) becomes 21. The only place that sourceType is mentioned is in a header file, where it is declared as public int[ ] sourceType. A size is never specified for it (anywhere in the program). Does anyone know why the program is acting as if the array has a size limit of 21 (indexes 0-20) when no size is ever declared?





for (iSource = 1; iSource %26lt;= es. numSources; iSource++)


{





es.sourceType [iSource] = 32;


}

Declaring an array in C++?
Those two variables are completely independent of each other.





You didn't provide the architecture, but I'm guessing that the one from the header file is a field in a class and the second one is inside a function somewhere as a local variable.





I'm guessing that the sourceType referenced in the for loop is the field one (public int[] sourceType) since it's being dot dereferenced (es.sourceType).





The reason you're getting the out-of-bounds error is because es.numSources is %26gt;= 21.





I've got a feeling that loop should read:


for (iSource = 0; iSource %26lt; es.numSources; ++iSource)


{


es.sourceType[iSource] = 32;


}





If the loop is correct then investigate why es.numSources (probably declared in a header as: public int numSources;) is larger than it should be.





Hope that helps.
Reply:All right, this code makes very little sense to me, but I'm going to give you a few of my thoughts on question 2.





1. es.numSources... That dot operator on es makes this look more like C# code than C++. Nonetheless since C++ does support classes, I'll assume C++ here.





2. Any C/C++ compiler I've ever used has required a size/initialization of the array before you use it. So at some point, this array IS being given a size.





3. In your typical for loop iterating through an array, you don't want your test to be "less than or equal to," just "less than."


Reason:


Array indexes start at 0. So in an array of 5 objects, you would have object[0], object[1], ... object[4].


object[5] is in fact the *sixth* object, and that's not possible in an array of size 5.





4. Your test case indicates you are looping until you've gone through all of es's integers. Later, in the loop code, you are attempting to access es.sourceType's objects. These are not the same. Unless you know with absolute certainty that the size of es.sourceType matches the size of es, you cannot hope to accomplish anything with this code.





So to summarize, try changing your code to read:





for (iSource = 0; iSource %26lt; es.numSources; iSource++)


{


es[iSource] = 32;


}





*OR*





for (iSource = 0; iSource %26lt; es.sourceType.numSources; iSource ++)


{


es.sourceType[iSource] = 32;


}





... and see what happens.