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.


What is the dev c++ solution, that has an array max of 40 elements......?

what is the dev c++ solution, that has an array max of 40 elements..the input should be from 0 to 9 only....and output the number according to its place value,, it shoud have a comma for each of the proper place value, the input should be outputed from up to down,,,,





here is the output:





Enter size: 4


4


0


9


6


Result: 4,096 (it should contain the comma (","),if it is greater than hundreds that pertains to its proper place value)





another example:


Enter size: 3


1


2


3


result: 123





another ex. hehehe:


Enter size:7


1


2


3


4


5


6


7


result: 1,234,567

What is the dev c++ solution, that has an array max of 40 elements......?
Hi Edrew,


Here is the MORE ACCURATE SOLUTION to your problem.


It will remove the shortcomings of the solution given by dear "iyiogrenci" just above my answer..








main()


{


int a[40];


int i,r,c,n;





printf("Enter total elements to be entered (1-40) : ");


scanf("%d",%26amp;n);





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


{


printf("Enter element a[ %d ]=",i);


scanf("%d",%26amp;a[i]);


}





printf("\n\n");





if(n%26lt;=3)


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


printf("%d",a[i]);


else


{


r=n % 3;


if (r != 0)


{


for (i=1;i%26lt;=r;i++)


{


printf("%d",a[i]);


}


printf(",");


}





c=n/3;





for(i=1;i%26lt;=c;i++)


{printf("%d%d%d",a[r+1],a[r+2],a[r+3])...


r=r+3;


if(i!=c)


printf(",");


}


}


}





.


.
Reply:#include %26lt;stdio.h%26gt;


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





main() {


int a[40];


int x,k,i,r,c,n;





printf("n=%",n);


scanf("%d",%26amp;n);





for(x=1;x%26lt;=n;x++) {


printf("a[ %d ]=",x);


scanf("%d",%26amp;a[x]);





}





printf("\n\n");


r=n % 3;


if (r != 0) {





for (i=1;i%26lt;=r;i++)


{printf("%d",a[i]);


}


printf(",");


}





c=n/3;





for(k=1;k%26lt;=c;k++)


{printf("%d%d%d",a[r+1],a[r+2],a[r+3]);


r=r+3;


printf(",");


}





printf("\n\n");


system("pause");





}








The output is in the form for n=5


12,345,





how can we delete the last character in the output


using backspace character?
Reply:How many times.... DEV C++ IS NOT A LANGUAGE!!!


C++ is the language you are coding in. Dev C++ is just an editor - you might as well ask "what is the Notepad C++ program for...". To my knowledge this will be the third time I have informed you of this fundamental fact.


How do you empty an array in C++?

I need to empty an array under a certain condition. When I declare the array, I declare it as:





char str[81];





I fill it up by reading in characters from a file, and, when I move to the next line in the file, I want to empty out all of those characters to fill it up again, with new characters.





Will adding a '\0' after the last character in the second set of characters have the same effect as re-initializing the array? Is there a better way?

How do you empty an array in C++?
I agree with "Jay".


u wont need to empty it. it'll just get overwritted when u read new line from the file. Adding NULL after the last character...denotes that the string should be read upto NULL.


So every thing u get a line read in the str, add a NULL at the end.





if u want to empty a string u can do this ..





str[0] = NULL;


%26lt;or%26gt;


str[0] = '\0';
Reply:Adding a '\0' after the last character has been placed in the array is a good idea since 99.9% C and C++ functions expect character array strings to be null terminated (otherwise they would not be able to know where the end of the string is?). But that's not reinitializing the array. There is no "reinitializing" needed. once you're done with that line, just start again at index zero and read the next line like you read the last one. A character array as declared is nothing more than a small block of memory.
Reply:use ReDim
Reply:the previous answer:


str = 0;


is a really bad idea, and the compiler shouldn't let you do it...





To answer your question, most functions in C++ will accept an array of characters in which there are some number of characters followed by a null character and treat that as a string.





So, for instance,





#include %26lt;iostream%26gt;





int main() {





char str[81];





str[0] = 'a';


str[1] = 0;





std::cout %26lt;%26lt; str;





return 0;


}





will print out the letter 'a'.





Assuming you are passing your array of characters to something that works this way (looks for a series of characters followed by a null character), the scheme you've decribed (setting "the last character in the second set of characters" to 0) should work for you.
Reply:str = 0;
Reply:When you declare char str[81] what you are really doing is allocating a block of 81 contiguous (sequential) characters, then setting str to point to that block. So, it's generally a bad idea to later assign str to NULL or 0.





As others have mentioned, you don't really need to reinitialize, just reuse the same array and it'll overwrite. However, if you're using the "array" as a string you'll want to add a '\0' to the end after reading each line, so it'll print out and work with string functions fine.





If you really want to initialize the array to some value, the fastest way is with memset(): memset(str, 0, 81); This will fill the memory block with zeroes (the second argument).





BTW, if you're using C++ you probably want to learn about the STL (standard template library) and use either a vector%26lt;char%26gt; or string object. It's much simpler and safer than using C-style arrays. For example:





string str = "";


str += 'a'; // append character 'a' to the end of the string


printf("%s\n", str.c_str()); // output with printf()


cout %26lt;%26lt; str %26lt;%26lt; endl; // output with c++ style iostreams


str.clear(); // clear the string.
Reply:one thing I need to suggest u is that there is no need to empty an array.


just initialize to some value like 'NULL' (zero is ascii value of null). It helps to verify whether an element is empty or not(in this case null or not).


What is the dev c++ formula that declares an array of 20 integers. Accept a number to determine the.........?

what is the dev c++ formula that declares an array of 20 integers. Accept a number to determine the number of 0 or 1 inputs(binary digits). Display decimal value using the output format:





Enter size: 4


1


0


1


1


Equivalent: 11


1x8=8


0x4=0


1x2=2


1x1=1

What is the dev c++ formula that declares an array of 20 integers. Accept a number to determine the.........?
Hello Edrew !!





Here is your solution:








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


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


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





void main()


{


int num[20]={0};


int total,i;


double sum=0;


clrscr();


cout%26lt;%26lt;"How many digits do you wish to Enter : ";


cin%26gt;%26gt;total;





if(total%26gt;0 %26amp;%26amp; total%26lt;=20)


{


cout%26lt;%26lt;endl%26lt;%26lt;"Please Enter "%26lt;%26lt;total%26lt;%26lt;" digits : "%26lt;%26lt;endl;


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


{


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


}








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


{


sum=sum + num[i]*pow(2,total-i-1);


cout%26lt;%26lt;endl%26lt;%26lt;num[i] %26lt;%26lt;"*"%26lt;%26lt;pow(2,total-i-1)%26lt;%26lt;"="%26lt;%26lt;num[i]*pow...


}


cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Equivalent is : "%26lt;%26lt;sum;


}


else


cout%26lt;%26lt;"Please Enter value between 0 and 20..";





getch();


}
Reply:Try searching the net for C++ tutorials and resources
Reply:Try searching the net for C++ tutorials and resources. It's worth bearing in mind that Dev C++ is an editor, not a language - the languages it edits are C or C++.

floral centerpieces

Writing an array and visiting each element of the array in C programming?

Well i don't understand how arrays really work, and I dont understand how to grab the element of the array, can someone


explain and show how to do this

Writing an array and visiting each element of the array in C programming?
Array id a data structure, it is a memory like in which u can store the datas of same type[ like all integers or all characters]


in adjacent locations..


Suppose u want to store marks of 5 subjects, u have to declare an array name 'sub' and of size 5, and its positions are 0, 1, 2, 3 ,4. now if u want to access the ur 3rd subject marks , u have to call it by the array name %26amp; its position as sub[2].


For more details, try google search...


All the best.......


How to return a multidimensinal array in C++?

i have the following array as private member in 1 class:





bool array[100][100];





i need to use the filled up array in another class so i need a get method to return the array. but





bool[][] getArray();





and bool* getArray();





both does not work..





thanks!

How to return a multidimensinal array in C++?
try to use : return array[0][0];
Reply:You may use one of the following methods:


1. Return a bool** like -


bool** getArray() {


return array;


}


2. Take in a reference -


void getArray(bool **%26amp;arr) {


arr = array;


}


However, you would also need to know the 1st (row) and 2nd(column) size of the 2d array. Hence the following would be a better solution -


--


void getArray(bool **%26amp;arr, int %26amp;row, int %26amp;col) {


arr = array;


row = n_rows;


col = n_cols;


}


How do I transfer an array table to a function as a parameter in the syntax programming of C++?

I just solve informatic problems in c++. I have a string array of data which I want to make some comparing with a temp string array so it can move on. That's easy. I use loops and ifs. But the problem is that I use it many times and I want to organize my program. Therefore, i intend to pass the string array of data as a parameter in a particular function. In fact, I would really be appreciated if I can transfer a parameter data type of a string array by reference. Anyways a lot of help would be appreciated.

How do I transfer an array table to a function as a parameter in the syntax programming of C++?
char strArray[][6] = {"AAAAA", "BBBBB", "CCCCC", "DDDDD", "EEEEE"};





int stringCount = sizeof(strArray)/sizeof("AAAAA");





int TestStringArray(char A[][6], char B[])


{


for(int i = 0; i %26lt; stringCount; i++)


{


if(0 == strcmp(A[i], B))


{


return i;


}


}


return -1;








}








void main()


{


char B[] = "EEEEE";





int result = TestStringArray(strArray, B);


}


Some questions in C programming for string and array?

In C, What is the diffrence between a string and an array?





Is it possible to execute code even after the program exits the main() function?


Is using exit() the same as using return?





What do you mean by binding of data and functions?

Some questions in C programming for string and array?
In c, typically, a string is a type of array (specifically it's an array of characters):





int myArray[100]={12};


char myString[100]="Hello!";





The difference is pretty much the same as the difference between an animal and a squirrel, i.e. One is just a type of the other.





I'm not sure if it's possible in C to have code execute after the end of the main function, but it's certainly possible in C++:





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


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





class MyClass


{


~MyClass()


{


printf(" World");


getch(); // Wait for a keypress...


}


};





MyClass c;





int main()


{


printf("Hello");


return 0;


}





What happens is that after the program ends, the global instance of MyClass, c, is destroyed, so it'll call it's destructor. If you run the above code you should see it print out the words "Hello World" (unless I've messed up somehow :) )





exit() is certainly not the same as return. exit() stops the entire program from running, whereas return just ends the current function being called. If you call return from the main() function, then it is pretty much the same though.





When people talk about binding data and functions, they normally mean building classes. Imagine that you have some data about people you want to keep (say, just their age and their name). So you can have two arrays, one of strings, one of numbers, to store the data. Then you can write some functions which use those arrays to print out the names and ages, or whatever. The alternate is to make a class called "Person" that contains a string and a number and has member functions for printing them out, in this way we have bound the data and functionality together.
Reply:I just checked, and I made a mistake in the code above. For it to compile you need to change:





class MyClass


{


~MyClass()





to





class MyClass


{


public:


~MyClass() Report It

Reply:I will try to keep it simple:





1. String is one datatype in which you can store one alphanumeric data of max 256 characters (Integers stores only numbers). Array is a datatype which can be classified as a string or integer or others depending on what kind of data you store in an array. Array is a collection of strings or other form of data. There are single dimention array, two dimensional array and multiple dimention array.





A program can exit the main function to go to another function, however if you exit main function as in end of function you cannot go to any other function. its end of program.





Exit function is not same as return. Return can allow you to come back to main function or go to another function. Exit function terminates the program/function.





Not sure about your last Q.
Reply:in C a string is an array of chars: for example





char* c = "the";


is the same as


char c = ['t','h','e','\0'];








when the main function exits the program will end.


using exit is generally used for errors, while return is generally used for normal execution
Reply:A string is an array of chars, an array can be integers, or any other data type,





It is possibole to execute code after the main function, but extremely difficult in C. You have to push code into memory, and then transfer control over to it, similiar to what you would do in the Assembly language. Most C programmers just use MAIN as a control function, and do all of the work in sub functions.





Binding of data and functions is an attempt at Object Oriented programming before C++.
Reply:check this link


it might help


http://www.google.co.in/url?sa=t%26amp;ct=res%26amp;...





regards


Islam Inamdar


islaminamdar@yahoo.com


inamdarinfotech.com


join new randomizer website


www.allyours.info
Reply:The difference is that a sting holds anything. It can be this "Junk","1234", "junk1234". You can't do calculations. With an array, it's like a place holder. It's usually like this. int stuff[10]. You use it to store information in it.





I don't think you can execute code after the Main(). However you can do a bunch of calls and functions with classes to do stuff in the main to clean it up. The rest i'm not sure.

wedding florist

Data stucture in C with Array Implementation of Stack?

#define __TEST__





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





#ifdef __TEST__


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


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


#endif


#define STACKSIZE 64








int stack[STACKSIZE];


static int stack_ptr=0;





void overflow_test( int pos )


{


if( pos %26gt;= STACKSIZE )


{


puts( "Stack overflow!\n" );


exit(1);


}


}





void underflow_test( int pos )


{


if( pos %26lt; 0 )


{


puts( "Stack underflow!\n" );


exit(1);


}


}





void push( int value )


{


overflow_test( stack_ptr+1 );


stack[stack_ptr++] = value;


}





int pop( void )


{


underflow_test( --stack_ptr );


return stack[stack_ptr];


}





#ifdef __TEST__





void wait4keypress( void )


{


while( !kbhit())


;


}





int main( void )


{


atexit(wait4keypress);


push(1);


push(2);


push(3);


printf( "%d\n", pop());


printf( "%d\n", pop());


printf( "%d\n", pop());


/* must cause stack underflow */


printf( "%d\n", pop());


return 0;


}





#endif /* __TEST__ */


Making an array in C++?

How do you make an array without pre-determining the amount of information entered?


My program needs to ask the user "Enter another integer? Y/N"...


But, i need to create an array for the set of numbers the user enters. I may not ask the user how many numbers they want to enter in the beginning, though.


Thanks!

Making an array in C++?
You have to dynamically allocate the array using pointers. My question to you, do you have to use an array or could you use a linked list? A linked list would be a much more efficent method at handling this issue.





Dynamically creating an array would required that you create an integer pointer and then assign the pointer to a new array.





int * myArray;


myArray = new int[count];





IF the array needs to resized (adding another integer) then you need to delete the array, hence freeing the space, then you need to create a new array to the pointer with count+1.





Delete [] myArray;


myArray = new int[count+1];





It has been FOREVER since I have done C++, so anyone feel free to correct me on this.
Reply:You can make a ADT (abstact data type) called a linked list. Funnily enough, these objects work more efficiently for insert operations and delete operations than regular arrays.





struct node


{ yourDataType data;


node *next;


};





node *start_ptr = NULL;





I'd write a nice concrete class and use it forever. Just look up linked list on the net.
Reply:Yes this is the most horrendous thing about C++ but its true ---





This feature isn't available in C++


you would have tried something like this --%26gt; int arr[ ][ ];


But it didn't work !!!! Right ????


So what u've done - predetermining the value by asking the user - is the only alternative left.
Reply:Use a linked list (such as the vector or list template classes in STL).





#include %26lt;vector%26gt;


#include %26lt;iostream%26gt;


using std::vector;


using std::cout;


using std::endl;





...





vector%26lt;int%26gt; myVec;


while( ...user answers yes...) {


int answer = ...get answer from user...


myVec.push_back(answer);


}





Now you can access myVec as if it were an array:





for(int i=0, j=myVec.size(); i%26lt;j; i++) {


cout %26lt;%26lt; "The " %26lt;%26lt; i %26lt;%26lt; "th entry is " %26lt;%26lt; myVec[i] %26lt;%26lt; endl;


}





If for some reason you MUST have an actuall array and not a list, you could create one at this point and copy the contents of myVec into it.
Reply:You need to have a temporary variable to read in the integer.


You also need to either allocate the array dynamically or


use one of the STL library list. If you use the allocate


array yourself than you need to reallocate everytime you get a new number.


Method 1) after you read in a number


++arraySize;


arrayPtr = new int [arraySize];


// now copy the old numbers into the new array





Method 2) search std::list this is a dynamically allocate list you don't have to readjust your list every time
Reply:int myarray [ ]


What is the dev c++ solution, that has an array max of 40 elements......?

What is the dev c++ solution, that has an array max of 40 elements......?


what is the dev c++ solution, that has an array max of 40 elements..the input should be from 0 to 9 only....and output the number according to its place value,, it shoud have a comma for each of the proper place value, the input should be outputed from up to down,,,,





here is the output:





Enter size: 4


4


0


9


6


Result: 4,096 (it should contain the comma (","),if it is greater than hundreds that pertains to its proper place value)





another example:


Enter size: 3


1


2


3


result: 123





another ex. hehehe:


Enter size:7


1


2


3


4


5


6


7


result: 1,234,567

What is the dev c++ solution, that has an array max of 40 elements......?
Hello Edrew, here is your solution :





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


main()


{


int a[40];


int i,r,c,n;


printf("Enter total elements to be entered (1-40) : ");


scanf("%d",%26amp;n);


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


{


printf("Enter element a[ %d ]=",i);


scanf("%d",%26amp;a[i]);


}


printf("\n\n");


if(n%26lt;=3)


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


printf("%d",a[i]);


else


{


r=n % 3;


if (r != 0) {


for (i=1;i%26lt;=r;i++)


{


printf("%d",a[i]);


}


printf(",");


}


c=n/3;


for(i=1;i%26lt;=c;i++)


{printf("%d%d%d",a[r+1],a[r+2],a[r+3])...


r=r+3;


if(i!=c)


printf(",");


}


}


}
Reply:Use the modulus operator(%). It takes two values and returns the remainder.





eg.


7 % 3 = 1


6 % 3 = 0


5 % 3 = 2


4 %3 = 1


3 % 3 = 0





I assume you store the inputted array size into an INT variable. Use that variable minus the array index modulus 3 to determine if it needs a comma.





eg.


for( int index = 0; index %26lt; arraySize; index++) {


if( (index %26gt; 0) %26amp;%26amp; ((arraySize - index) % 3 == 0)) {


cout %26lt;%26lt; ",";


}


cout %26lt;%26lt; arrayOfNumbers[ index];


}


How to resize an array in C++?

Hey, I just need to create a basic resize array method. I'm not sure how to do it though.





int array[ ];


//declare array





I'm thinking....





int size;


int array[ size ];


//Is this the correct way or is this defining another array? Thanks.

How to resize an array in C++?
you need to use dynamic memory (lookup 'new' in your book)
Reply:You probabily don't need to use the new operator, with the large cache sizes on computers, arrays are very efficient. The new operator is dynamic but requires knowledge to use it properly in your program. Things could get messy if your not familiar with employing your own memory management (procedurally allocating/deallocating memory). Great if your program uses a huge amount of memory and needs items sorted by their value. Especially great if you are implementing with an abstract data type.


You don't have to use the new operator but you can't change the size of an array once initialized.


Roughly, what is the largest number of elements your array will hold at any one time? Initialize your array with this value + 5% just in case, but not necessary if you know exactly. Performance will drop on a 8k cache if your array houses millions of elements. Your safe to allocate loads even if unused.
Reply:unless your need for speed is critical, and you assume all array management, you might want to look into the stl collection classes like vector, which is a dynamically growing array.

local florist

Write a simple C/C++ program which defines an array of five elements, then using pointer to display the addres

Write a simple C/C++ program which defines an array of five elements, then using pointer to display the addresses of all elements of the array.

Write a simple C/C++ program which defines an array of five elements, then using pointer to display the addres
#include %26lt;iostream%26gt;





using namespace std;





int main()


{


int array[5];


int* pointer = NULL;





for(int i = 0; i %26lt; 5; i++)


{


pointer = %26amp;array[i];


cout %26lt;%26lt; "The address of element (" %26lt;%26lt; i + 1 %26lt;%26lt; ") is: ";


cout %26lt;%26lt; %26amp;(*pointer);


}


return 0;


}





// you can also do it without using a pointer as follows:





for(int i = 0; i %26lt; 5; i++)


{


cout %26lt;%26lt; "The address of element (" %26lt;%26lt; i + 1 %26lt;%26lt; ") is: ";


cout %26lt;%26lt; %26amp;array[i];


}
Reply://C//or//C++


-------------------------------


Start


Define: /Array


Elements: /5


Now use:


What?


pointer to address the F* display


what for?


elements of the display?


how many?


5!!!


------------------------------


end
Reply:Any other help me to solve that Question
Reply:/* Tolga's C code */





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


int main(){


int i=0, arr[5];





for(;i%26lt;5;i++)


printf("Address of the element #%d is %p\n",i,(arr+i));


return 0;}
Reply:dude, that's not even a program. that's like 5 lines of code, if even.
Reply:#include%26lt;iostream.h%26gt;


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





int main(){


int *p;


int a[5] = {1,2,3,4,5};





p = a; //simple assignment





for(int i = 0;i%26lt;5;i++)


cout%26lt;%26lt;(p+i);





return 0;


}
Reply:Do your own homework
Reply:Why dont you try your own Mind to Answer your Assignments, you are just sitting in your Campus to ask people to do Assignments for you, hmmmm








Very Bad Hafsa, Very Bad
Reply:/*-------hi dude: try this :its 100% right---------*/


/*-------it really takes something to learn pointers in c---*/


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


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


void main()


{


int a[5] = {1,2,3,4,5};


int *i;


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


{


printf("\t%d",*(a+i));


}


getch();


}


Write a simple C/C++ program which defines an array of five elements, then using pointer to display the addres

Write a simple C/C++ program which defines an array of five elements, then using pointer to display the addresses of all elements of the array.

Write a simple C/C++ program which defines an array of five elements, then using pointer to display the addres
/* Tolga's C code */


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


int main(){


int i=0, arr[5];





for(;i%26lt;5;i++)


printf("Address of the element #%d is %p\n",i,(arr+i));


return 0;}
Reply:This looks like a command instead of a question. I don't even see a question mark anywhere! You wouldn't be asking people to do your homework now, would you? You're not that kind of person I'm sure.


C++ program Array help please!!!??

I am supposed to write a program that asks the user for a file name. The program should read the contents of the file and display the following data:


the lowest number, the highest, the total of the numbers, and the average of the numbers.


My program will compile but it never reads the numbers from my "numbers.txt" file. It just gives me random numbers. My teacher wants us to use functions for everything, but I don't get what to do in the function readNumbers. My teacher completely left us on our own to do this. He just walked out of class so can anyone please help me with this?

C++ program Array help please!!!??
FINAaaaaaaaaaaaaLLY I solve the problem





i hope that matches your expectations :)








#include "stdafx.h"


#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;iomanip%26gt;








using namespace std;





void readNumbers(ifstream %26amp;, int [], int);


int getLowest (int [], int);


int getHighest (int [], int);


int sumArray (int [], int);


double getAverage (int [], int);





const int ARRAY_SIZE = 12;








int numbers[ARRAY_SIZE]; // array as global


// to keep the alteration of function read number





int main()


{





int total;


double average;


int highest;


int lowest;





ifstream inputFile;





readNumbers(inputFile,numbers,ARRAY_SI...





lowest = getLowest(numbers, ARRAY_SIZE);





highest = getHighest(numbers, ARRAY_SIZE);





total = sumArray(numbers, ARRAY_SIZE);





average = getAverage(numbers, ARRAY_SIZE);





cout %26lt;%26lt; "The lowest number is: " %26lt;%26lt; lowest %26lt;%26lt; endl;


cout %26lt;%26lt; "The highest number is: " %26lt;%26lt; highest %26lt;%26lt; endl;


cout %26lt;%26lt; "The total of the numbers is: " %26lt;%26lt; total %26lt;%26lt; endl;


cout %26lt;%26lt; "The average of the numbers is: " %26lt;%26lt; average %26lt;%26lt; endl;











return 0;





}








//**************************


//Definition of readNumbers*


//**************************





void readNumbers(ifstream %26amp;inputFile,int numbers [], int arraySize)


{


int count;





inputFile.open("C:\\numbers.txt");





if (!inputFile) {


cerr %26lt;%26lt; "Unable to open file";


exit(1); // call system to stop


}








for (count = 0; count %26lt; ARRAY_SIZE; count++)


inputFile %26gt;%26gt; numbers[count];





inputFile.close();





cout %26lt;%26lt; "The numbers are: ";


for (count = 0; count %26lt; ARRAY_SIZE; count++)


cout %26lt;%26lt; numbers[count] %26lt;%26lt; " ";


cout %26lt;%26lt; endl;


}








//************************


//Definition of getLowest*


//************************





int getLowest(int Narray[],int size)


{


int lowest;





lowest = Narray[0];


for (int count = 1; count %26lt; size; count++)


{


if (Narray[count] %26lt; lowest)


lowest = Narray[count];


}


return lowest;


}








//*************************


//Definition of getHighest*


//*************************





int getHighest(int Narray[], int size)


{


int highest;





highest = Narray[0];


for (int count = 1; count %26lt; size; count ++)


{


if (Narray[count] %26gt; highest)


highest = Narray[count];


}


return highest;


}





//***********************


//Definition of sumArray*


//***********************








int sumArray(int Narray[], int size)


{


int total = 0;





for (int count = 0; count %26lt; size; count++)


total += Narray[count];


return total;


}





//*************************


//Definition of getAverage*


//*************************





double getAverage(int Narray[], int size)


{


double average;





int total = 0;





for (int count = 0; count %26lt; size; count++)





total += Narray[count];





average = total / ARRAY_SIZE;





return average;


}
Reply:thanks alot my pleasure usman :) Report It

Reply:First, I don't have the complier installed on my machine. But your code does look right. The only thing that I saw that might help is where does it open the file name that you enter. I see where it takes it in, but I don't see where it is used later in the code. I hope that does help.





Here is something you have


"cin %26gt;%26gt; userFile;" where it should be "cin%26gt;%26gt; inputFile;"

floral deliveries

C programming array question?

Here's a sample of a program:





char cur[6][20]={"Euro ", "YEN ", "Franc", "ZWD", "DOP", "Exit"}; //five currencies types plus exit option (6)


float rates[5] ={1.3859, 114.9750, 0.8424, 0.00003333, 0.02988}; // five (5)exchange rates








What would the (20) relate to if we are doing currency conversion? Why should the number (20) be used in this example if the conversion is hard-coded why not any other number?

C programming array question?
The cur[6] relate to the five currency options (+Exit) in the array, and [20] is the size of the string in each of the arrays, so the longest currency /option name is "Franc" that is at cur[2]. So there is a 'wasted space' for minimum 14 bytes (1 byte required for string terminating null character \x0) for each array from [0] to [6].


Therefore your declaration may be like char cur[6][6] = {"Euro".......};
Reply:u can use 6 in place of 20 try it


20 is used for length of maximun characters in char variable
Reply:Hi,


Of course, 20 is the maximum length of the string that can be stored in cur[6][20] array. But there is wastage of space. Only "Franc" is using max space, i.e., 5 charachter. Still there is wastage of 15 characters.





Instead of above u could declare a character array of pointers that would save space without writing any fixed size (in this case 20). Still u can save string of any length.





Alternative code of ur code is:


char *cur[6]={"Euro","Yen","Franc","ZWD","DOP...


u can access any of the elements by writing cur[4], cur[0] etc.
Reply:Hi,


I am no expert but i think it's the "Text String Length" in the array,


Write a program In C++ which defines an integer array of size 8.?

Write a program In C++ which defines an integer array of size 8.The array size should be defined by a constant variable.


Read numbers from keyboard for this array.


Interchange the values at position 2 and 7 and then display the array.





Output:


Enter the values for array :


6


5


2


4


8


3


7


12


The values of array after interchange :


6 7 2 4 8 3 5 12

Write a program In C++ which defines an integer array of size 8.?
c++ code:





void main()


{


int arr[8];


cout%26lt;%26lt;"Enter the array values : ";


for(int i=0;i%26lt;=7;i++)


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





int temp;


temp=arr[1];


arr[1]=arr[6];


arr[6]=temp; //interchanging 2nd and 7th values of array





cout%26lt;%26lt;"\n The array now is: \n"


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


cout%26lt;%26lt;" "%26lt;%26lt;arr[i];


}
Reply:For your homework questions, please try to post what you can do yourself so that people can help you, not do your homework for you.


C++ taking data from an array?

for my c++ class one of the questions ask to take scores from an array(its a 3 by 4) and to see which one is the greatest in each row and each column….its really simple but I have completely spaced how to take the numbers from the array and compare two of them at a time. If you can help please do!!! Thanks!

C++ taking data from an array?
this is all there is to it...





int x=0, y=0;


int array[3][4];


int num1, num2;








num1=array[x][y];


num2=array[x][y+1];





if(num1 %26gt; num2)


{


//...do something


}
Reply:This sounds like a rather easy thing to do. Here's a link to some relevant code, taking the two-dimensional array 'scores' and outputting the maximum to the arrays 'rowmax' and 'colmax':


http://pastebin.com/m1d0e1058


No guarantees, though; I'm new to C++ too. And even if this code does work, there might be a more efficient way to do it.


Casting string to array in C#.net?

i have done this casting.


what's wrong with it?


exception is-unable to cast string type to array


public Array getRange(string startCell, string endCell)


{





Range range = currentWorksheet.get_Range(startCell, endCell); //Get the cells


//Array array = (System.Array)currentWorksheet.get_Range... endCell);


System.Array array = (System.Array)range.Cells.get_Value(null... the values of the cells


return array;


}

Casting string to array in C#.net?
Umm, it'd help if you say what type of array you're trying to create. If you just want an array of chars then use ToCharArray:





string myString = "This is a string";


char[] myChars = myString.ToCharArray();

buy flowers

Write a C Program to declare an array for 2 0 floats.?

Write a C Program to declare an array for 2 0 floats. Accept the values from the user sort the two arrays in descending order. Merge the two arrays into a new array and display the new array.

Write a C Program to declare an array for 2 0 floats.?
what did you mean by 2 0 floats? is it 20 floats or 2.0 floats or what?





if you said 2D floats or 2 array of floats, i understand.





Let say you need to declare a 2D floats, so it will become like this:





float twod_flArray[20][20];





or





float twod_flArray[2][2] = {{1.0,2.0},{2.0,3.3}};





Sorting problem may be done by using a bubblesort algorithm.


There are a lot of it in the net, try to search.


To merge it, just append into the new array.
Reply:Homework I gather...


I'm guessing you would get a better response if you asked for help -vs- what appears to be a command to write it for you


Take care





Might also look into joining or creating a study group, good social / team building as well as helpful for study.


How do you declare an array of vectors in c++?

How do you declare an array of vectors in c++?

How do you declare an array of vectors in c++?
Actually, I think that this will do the trick:





vector%26lt;string%26gt; a_vStr[10];
Reply:"Array" and "vector" are the same thing. If you mean STL vector, it can be so:


typedef vector %26lt;int%26gt; int_vec;


typedef vector %26lt;int_vec%26gt; int_vec2;


This way, int_vec2 is vector of vectors, or array of vectors.


Hi, i m getting "Array Bound Read" in C language for perticular file that is written in C.i m using memcpy().

I m getting "Array Bound Read" error while usign memcpy () in C language.


can u plz tell me why this err is coming and How can we solve these kind of errors.


That err is displaying while i m chcking through Rational Purifier s/w.


please let me know how we can resolve this err.ASAP





Thanks and Regards,


Sharique

Hi, i m getting "Array Bound Read" in C language for perticular file that is written in C.i m using memcpy().
It means, you have crossed array boundary and try to read from another memory block.


Validate your memcpy() parameters.


Can some body help me, why i am getting error in this array c pgm?plz help me.thanks in advance.?

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


{


void print(int *arr_beg, int *arr_end);





while(arr_beg! = arr_end)


{


printf("%i",*arr);


++arr_beg;


}


}





void main()


{


int arr[] = {0,1,2,3,4,5,6,7,8,9}


print(arr,arr+9);


}

Can some body help me, why i am getting error in this array c pgm?plz help me.thanks in advance.?
soem little problems with {}.. try this one:


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





void print(int *arr_beg, int *arr_end){


while(arr_beg! = arr_end)


{


printf("%i",*arr);


++arr_beg;


}


}





void main()


{


int arr[] = {0,1,2,3,4,5,6,7,8,9}


print(arr,arr+9);


}
Reply:1)instead of *arr you should write *arr_beg(in print line 3)


2) ; is missed in you main(first line)


3)correct the place of {} as the person above said


hope it helps

floral

There are n1/2 copies of an element in the array c[1..n]. Every other element of c occurs exactly once. If the

Ok, half the question appears to be missing. But I suggest you post this in mathematics next time, based on how your question starts it looks like set theory to me.


How do i display contents; grabbed from textboxes, then put into an array, using a label control, using c#?

This is using c#


here is the array


%26lt;script runt="server"%26gt;


void Page_Load(object sender, EventArgs e)


{


String[] userdata = new string[19];


userdata[0] = "Page2Text1";


userdata[1] = "Page2Text2"; etc


userdata[18] = "Page2Text19"


}


%26lt;/script%26gt;


I have a button i'm going to press, so i'll need an event handler that'll take whats in the array which is from various textboxes as you can see from above and i want to display the contents in a label control.

How do i display contents; grabbed from textboxes, then put into an array, using a label control, using c#?
Are you loading everything to the same page, or are you redirecting to a new page?


Finding the mode of an array C++?

I have a vector that records the number of occurances based on user input, but I cannot figure out how to find the mode of that data here is my code listing the occurances and what I have so far for the mode:





cout%26lt;%26lt;"Roll"%26lt;%26lt;" "%26lt;%26lt;"Count"%26lt;%26lt;endl;


for(num = 1; num %26lt;=50;num++){


cout%26lt;%26lt;num%26lt;%26lt;" "%26lt;%26lt;Count[num]%26lt;%26lt;endl;


}





void Mode(CountType %26amp;Count, int %26amp;num, int %26amp;i)


{





int pos_mode;


int mode;


for(pos_mode = 1; pos_mode %26lt;=50;pos_mode++){


if (Count[pos_mode] %26gt; Count[num]){


mode = pos_mode;





}

















}


cout%26lt;%26lt;"The mode is: "%26lt;%26lt;mode;


}

Finding the mode of an array C++?
Try this:





I'm assuming CountType to be integer.





void Mode(CountType* Count)


{


//first sort the count array yourself





int n = 0;


int count = 0;


int max = 0;


int mode = 0;





for(int i = 0;i%26lt;50;i++)


{





if(n != Count[i])


{


n = Count[i];


count = 1;


}


else


{


count++;


}





if(count %26gt; max)


{


max = count;


mode = n;


}





}





cout%26lt;%26lt;"Mode: "%26lt;%26lt;mode%26lt;%26lt;endl;


}








Don't forget to sort the array first, it is important.


Saving the enter key in array..c++??

cin.get cant do that nor can cin%26gt;%26gt;


so how do i do it??

Saving the enter key in array..c++??
cin%26gt;%26gt;"here witte the arry as you predifine"





this can save your arry.

daisy

Write a program in C using one-D. array that prints the difference of the five input values from the lowest.?

Write a program in C using one-dimensional array that determines the lowest among five input values and prints the difference of the five input values from the lowest.





Sample input: 45793


Sample output:


1


2


4


6

Write a program in C using one-D. array that prints the difference of the five input values from the lowest.?
#include%26lt;stdio.h%26gt;


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





#define INPUT_SIZE (5)





int main()


{


int nIndex;


int Input[INPUT_SIZE];


int nMinIndex = -1;


int nMinVal = 10;





printf("Enter Number of 5 digits:");


for(nIndex = 0; nIndex %26lt; INPUT_SIZE; nIndex++)


{


scanf("%1d",%26amp;Input[nIndex]);


/* Save Lowest Value */


if(Input[nIndex] %26lt; nMinVal)


{


nMinVal = Input[nIndex];


nMInIndex = nIndex;


}


}


printf("Output:");


for(nIndex = 0; nIndex %26lt; INPUT_SIZE; nIndex++)


{


if(nIndex != nMInIndex )


{


printf("%d\r\n", Input[nIndex] - nMinVal);


}


}


return (0);


}
Reply:The easiest way to do this would be to loop through the array once, finding the lowest value, then loop through again printing out the differences.





It should only take a few lines of code.
Reply:#include %26lt;stdio.h%26gt;


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





int


compare(const void *elem1,const void *elem2){


if((*(int *)(elem1)) %26lt; (*(int *)elem2))


return(-1);


if((*(int *)(elem1)) %26gt; (*(int *)elem2))


return(1);





return(0);


}








int


main(int argc, char *argv[]){


int ii=0;


int arr[6]={0,0,0,0,0};





/* Handle under flow or overflow */


if(argc%26lt;2 || argc%26gt;6)


return(-1);





for(ii=0;ii%26lt;(argc-1);ii++)


arr[ii]=atoi(argv[ii+1]);





qsort((void *)arr,5,sizeof(int),compare);





for(ii=1;ii%26lt;5;ii++)


printf("%d\n",arr[ii]-arr[0]);





return(0);


}
Reply:#include%26lt;math.h%26gt;


main()


{


int i,a[5];


clrscr();


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


{


printf("Enter the number::");


scanf("%d",%26amp;a[i]);


}


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


printf("%d",abs(a[i]-a[i+1]));


getch();


}


C++ Help..... Can an array contain values of different data types?

C++ Help..... Can an array contain values of different data types?

C++ Help..... Can an array contain values of different data types?
Technically no, however, you have the option of using the pointer data type so that each pointer in the array points to a different object that's been instantiated.





You would somehow need to know ahead of time what data types are associated with each index position.





Another option is to polymorphism if the objects are similar and can be derived from the same superclass or perhaps they simply have the same interface.





Another option is to use a variant data type that can represent these objects.
Reply:Definition of array is "holding any number of data in the same data type with consecutive memory. So, we can't create an array with different number of data types. Because each element should have the equal memory size.





There are two to do like that


1) Create void pointer array with number of elements as you need. And we can give the address of the any type of variable to each element of array, Even we can user defined data type and user defined class variable


void *poin[10];


int a=1;


float b=2.56;


chat *c="yahoo answers";


poin[0]=%26amp;a;


poin[1]=%26amp;b;


poin[2]=c;


cout%26lt;%26lt;*(int*)poin[0]; //type casting and printing


cout%26lt;%26lt;*(float*)poin[1]; //type casting and printing


cout%26lt;%26lt;(char*)poin[2]; //type casting and printing


Length can be applied by user need


2) create a struct variable with different variable


struct exam


{


int rno;


char *name;


float avg;


}


struct exam e;


e.rno=10;


e.name="yahoo answers";


e.avg=68.52;


//in this way number of variable can be changed after allocating the struct variable





Try this if you have a doubt mail me. I will be with you regarding this.
Reply:not in c++, someother languages do allow it though
Reply:no an array cannot contain values of different data types.since an array is a collection of elements of homogeneous data type.


What could I do to copy a string into a multiple array string in c++?

I am writing a program in c++, I would like to copy a srting into a multiple array string. i have used the srtcpy() function but Iam getting an error message telling me that :


-invalid conversion from 'char' to 'char*'


-initializing argument 1 of 'char*' strcpy(char*, const char*).








//this is what I get as message. and here is my program:


# include%26lt;iostream%26gt;


#include%26lt;string%26gt;


using namespace std;


int main()


{int namsize;


char names[20][20], newname[18] ;


cout%26lt;%26lt;"what is the size of your name";


cin%26gt;%26gt;namsize;


cout%26lt;%26lt;" please enter the name";


cin.getline(newname,namsize);


for (int i=0;i%26lt;=(namsize+1);i++)


strcpy(names[i][1],newname[i]);


system("pause");


return 0;


}


please could you help me to solve this problem? thanks

What could I do to copy a string into a multiple array string in c++?
do strcpy(names[i],newname) instead of


strcpy(names[i][1],newname[i])


and the for loop should go from 0 to 19.





for (int i=0;i%26lt;20;i++)


strcpy(names[i],newname);








( i am assuming you want to create 20 copies of the same name in the array)


strcpy has the loop built into it. you just need to pass the pointer to the char array to it ,terminated by null of course.(If u din get the last part, its ok)
Reply:this is a syntax error! you need spaces between char names [20] [20]..that should work!
Reply:/*


just do copy of one string onto the next


eg. shown below is a c++ code


*/


# include%26lt;iostream%26gt;


#include%26lt;string%26gt;


using namespace std;





int main()


{





int namsize;





string names="fanda", newname ;


newname=names;





cout %26lt;%26lt;"\nnames: "%26lt;%26lt;names;


cout%26lt;%26lt;"\nnewname: "%26lt;%26lt;newname;


cout%26lt;%26lt;endl;





system("pause");


return 0;


}
Reply:You are using C++. Seriously consider using std::vector and std::string. Then you don't have to ask really odd questions like "what is the size of your name".





Also consider joining a YahooGroup for C/C++ programming instead of using Yahoo Answers for questions like this. You'll learn a LOT more that way by being part of a community who shares the same interests and goals.


Anyone, kind enough to help with a C++ parallel array?

I need to display the price and quantity of the item whose ID is entered by the user. Below is what I have but it only displays some number I have no idea where it is coming from. -858993460 for both prices quantities





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using std::string;


using std::cout;


using std::cin;


using std::endl;





int main()


{


//cin %26gt;%26gt;


//declare arrays


string id[5] = {"10", "14", "34", "45", "78"};


int prices[5] = {125, 600, 250, 350, 225};


int quantities[5] = {5, 3, 9, 10, 2};


string searchForID = "";





cout %26lt;%26lt; "Enter Product ID: ";


getline(cin, searchForID);





while (searchForID != "5")


{


int y = 0; //keeps track of array subscripts


while (y %26lt; 5 %26amp;%26amp; id[y] != searchForID)


y = y + 1;





if (y %26lt;= 5)


cout %26lt;%26lt; "Prices: $ " %26lt;%26lt; prices[y] %26lt;%26lt; endl;


cout %26lt;%26lt; "Quanty: $ " %26lt;%26lt; quantities[y] %26lt;%26lt; endl;








cout %26lt;%26lt; "Enter ID : ";


getline (cin, searchForID);





}





return 0;


} //end of main function

Anyone, kind enough to help with a C++ parallel array?
#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using std::string;


using std::cout;


using std::cin;


using std::endl;





int main()


{


//cin %26gt;%26gt;


//declare arrays


string id[5] = {"10", "14", "34", "45", "78"};


int prices[5] = {125, 600, 250, 350, 225};


int quantities[5] = {5, 3, 9, 10, 2};


string searchForID = "";





cout %26lt;%26lt; "Enter Product ID: ";


getline(cin, searchForID);





while (searchForID != "5")


{


int y = 0; //keeps track of array subscripts


while (y %26lt; 5 %26amp;%26amp; id[y] != searchForID)


%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;


wats this loop for


%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;


y = y + 1;


.........................................


after this while loop terminates y=5


so


.........................................





if (y %26lt;= 5)


cout %26lt;%26lt; "Prices: $ " %26lt;%26lt; prices[y] %26lt;%26lt; endl;


cout %26lt;%26lt; "Quanty: $ " %26lt;%26lt; quantities[y] %26lt;%26lt; endl;


.........................................


price[5] and qualities [5] are displayed which are garbage values


.........................................





cout %26lt;%26lt; "Enter ID : ";


getline (cin, searchForID);


.........................................


and wats this for


%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;%26gt;


}





return 0;


} //end of main function

gladiolus

Need help with implementing C that takes an array of integers,& and reverses the order of items in the array?

Implement a function in C that takes an array of integers,%26amp; and reverses the order of items in the array?


The function you implement must have the following form:


void reverse_order(int array[], int n)


{


/* your code goes here: re-order the n items in the array so that the


first one becomes the last


* * and the last one first.


*/


}


You should then implement a main() function which reads in a set of integers into an array,


reverses the order of the number (using the function you have just defined) and prints them out


again.


For example, when complete and compiled, you should be able to run your program, and enter


the numbers:


2 4 5 11 23 1 4


then program will then print out:


4 1 23 11 5 4 2


__,_._,___

Need help with implementing C that takes an array of integers,%26amp; and reverses the order of items in the array?
simple in the reverse function just make another array that will copy the orignal array backwards


j=0;


for(int i=sizeof(array)-1;i%26gt;=0;i--){


modarray[j] = origarray[i];


j++;


}
Reply:Use a for loop


swap a[i] with a[n-i-1]





a[0] a[5-0-1]
Reply:Well now you have the reverseArray function. Does it all work now????


;-)


Program to find the reverse of each value of array elements in c-language?

program to find the reverse of each value of array elements in c-language

Program to find the reverse of each value of array elements in c-language?
I'm not sure what you mean by 'reverse'.





Generally with an array you can use a for loop to iterate through it.


Program to find the reverse of each value of array elements in c-language?

program to find the reverse of each value of array elements in c-language

Program to find the reverse of each value of array elements in c-language?
You mean reverse the ordering of elements in the array (e.g., (1, 2, 3) -%26gt; (3, 2, 1))?





If so, you'd iterate up to half the length of the array, and exchange array[i] with array[length - 1 - i]
Reply:The most efficient way to do a dictionary look-up of finding an element's location in an array is to maintain a hash table, although a binary search will do the job well enough for sorted arrays.
Reply:what kind of array? int? boolean?


what do you mean by reverse?


C++programme to accept 10 elements into an array then find out a particular number?

it is a c++ programme about arrays

C++programme to accept 10 elements into an array then find out a particular number?
#include%26lt;iostream.h%26gt;


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





void main()


{


int a[10],ele,i;


clrscr();


printf("\n");


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


{


printf("Enter a[%d] - ",i);


scanf("%d",%26amp;a[i]);


}


printf("Enter number to be searched - ");


scanf("%d",%26amp;ele);


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


{


if(a[i]==ele)


break;


}


if(i%26lt;10)


printf("Number is present");


if(i==10)


printf("Element not present");


getch();


}





It will work for you...

narcissus

C++ class/array program?

For this program, you will create a student class and a main program that reads student information from a file and outputs the students' names in groups based on grades.





name grade





Name is a string type and grade is a char type from the set {A,B,C,D,E}.


Requirements:





Your main function should should read in the file until eof is reached, instantiate a new object for each student using your student class, and output the results.


Your student class consists of 2 private variables (name and grade), one constructor and the following 3 functions:


void setGrade(char);


string getName();


char getGrade();


The constructor for your class should set the student's name - student(string)


After constructing an object for a student, you need to read in the grade and call the setGrade(char) function to set the grade for that student.


To form the output, you need to call the getName() and getGrade() functions for each student.

C++ class/array program?
[UPDATED AGAIN. SORRY THERE WAS AN ERROR WITH MY CONSTRUCTORS. TRY AGAIN]





Ok, yes you can place everything in one file. Here is how:





#include %26lt;fstream%26gt;


#include %26lt;string%26gt;





using namespace std;





class Student


{


public:


Student();


Student(string someName);


void setName(string someName);


string getName();


char getGrade();


void setGrade(char someGrade);


private:


string Name;


char Grade;


};





Student::Student()


{


Name = " ";


Grade = ' ';


}





Student::Student(string someName)


{


Name = someName;


Grade = ' ';


}





void Student::setName(string someName)


{


Name = someName;


}





void Student::setGrade(char someGrade)


{


Grade = someGrade;


}





string Student::getName()


{


return Name;


}





char Student::getGrade()


{


return Grade;


}








int main()


{


ifstream inFile;


ofstream outFile;


const int MAX_STUDENTS = 50;


const int NUM_GRADES = 5;


Student arrayList[MAX_STUDENTS];


char grades[] = {'A', 'B', 'C', 'D', 'E'};


int count = 0;


string name;


char grade;





inFile.open("C:\\students.txt");





while(!inFile.eof() %26amp;%26amp; count %26lt; MAX_STUDENTS)


{


inFile %26gt;%26gt; name %26gt;%26gt; grade;


arrayList[i] = Student(name);


arrayList[i].setGrade(grade);


count++;


}





inFile.close();


outFile.open("C:\\groups.txt.");





for(int i = 0; i %26lt; NUM_GRADES; i++)


{


outFile %26lt;%26lt; grades[i] %26lt;%26lt; " ";


for(int j = 0; j %26lt; count; j++)


{


if(arrayList[j].getGrade() == grades[i])


outFile %26lt;%26lt; arrayList[j].getName() %26lt;%26lt; " ";


}


outFile %26lt;%26lt; endl;


}


outFile.close();


return 0;


}





// Please copy %26amp; run the code. It should work. Let me know


// if it works or not, please. Also, let me know if you need


// anything else.





// One more thing though, you are not checking whether or not


// opening the input %26amp; output files fails. Do you want me to


// show you how?





Hey Lisa, you said you owe me, and I agree. I need a favor from you. I want you to learn the following because you made mistakes related to these:





1) When you open a file don't close it until you are done with it. "Done with it" means that you already read data from the file or wrote data to the file.





2) All of your constructors have to have the same name as the name of the class (case sensitive). In your case, all of them have to be named "Student" not "student".





3) When your member function does not require parameters such as your getName function, here is how you declare it:





string getName();





NOT





string getName(void);





4) When your function requires a parameter such as your setGrade function, this is how it should look like:





void setGrade(char someGrade);





NOT





void setGrade(char);





Always name your parameters.
Reply:#Include %26lt;fstream.h%26gt;


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





void main()


{


ifstream ifs("students.txt");


string line;


Student S[100];


while(!ifs.eof()) {


getline(ifs,line);


cout %26lt;%26lt; "[ " %26lt;%26lt; line %26lt;%26lt; " ]" %26lt;%26lt; endl;


//put ur logic like


S(First 10 Charecter name , Rest all charets Number)


then call a sort Program


}


if you can wait then pls. I can write program after 5:30. as if I am in office now.


How to delete the object element of array of object ? C++ programming?

like we have array of employees


after entering the data..


when we enter something employee ID


%26amp; we need to delete that employee object from array of objects


.....C++ programming

How to delete the object element of array of object ? C++ programming?
Dont use an Array, Make a Linked List... u can easily add,delete, insert into the list

statice

Example of multi dimensional array in turbo c?

i dont know how to manipulate array in turbo c

Example of multi dimensional array in turbo c?
An array is a variable that has space to hold 1 or more instances of only ONE data type.


Example:


unsigned int fuzz[256];


This allocates enough memory for 256 unsigned integers, referenced by the variable name fuzz.


You can store up to 256 unsigned integer numbers in that array. Example: 4,5,7,10,etc.


If you want to write a number to the array, you would write something like: fuzz[2]=4;


In this case, the unsigned integer 4 is written to the 3rd location of array fuzz (array numbers start at 0, not 1!).





A multi-dimensional array is like a matrice.


Using my previous example, you can think of a single array as 1 row of numbers. A multi-dimensional array can be thought as having multiple rows of data.





Example:


unsigned int foo[256][256];


This is a two dimensional array.


Think of it as 1 row of numbers, with another row of numbers beneath it.


To write to the first location in the SECOND row, you could do this: foo[1,0]=4.


The first number in the bracket represents the row number (0-1 in this case) and the second number represents the location on that particular row. The number 4 is written to the second row, 1st column on that row.
Reply:yes


Hi, I have a c++ parallel array (4 arrays) program to write. In three of the arrays the user inputs different

data types. The first is string array, second is char, third float, and the fourth calculates from the second and third arrays. Then it has to display array 1, 2 and 4. Oh, and I have not mentioned about input validations yet. Please! I need help. anything at all


Thank you.

Hi, I have a c++ parallel array (4 arrays) program to write. In three of the arrays the user inputs different
You need a for loop that executes the same number of times as the length of the arrays (which should all be the same length).





declare your variables


for(int i = 0; i %26lt; length of arrays; i++)


{


cin %26gt;%26gt; all of your variables, with appropriate prompts to the user;


validate inputs;


store the inputs in arrays 1, 2, and 3 at index [ i ];


calculate the value for array 4 and store it at index [ i ];


}


for(int i = 0; i %26lt; length of arrays; i++)


{


cout %26lt;%26lt; arrayOne[ i ] %26lt;%26lt; " " or whatever your formatting is;


}


do the second for loop for arrays 2 and 4


C++ problem. array n=[2,4,6,8,10,29,11,12]; array o=[49,48,46,45,44]. need 2 select a random number 4rm both?

has to kinda look like a c++ code.


something like this:


#include%26lt;iostream%26gt;


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


#include%26lt;iomanip%26gt;


using namespace std;





int main()


{


int n=[2,4,6,8,10,29,11,12,13,14,15,16,18,19...


int o=[49,48,46,45,44,43,42,39,38,37,36,33,3...


d1=math.rand()*[n];


d2=math.rand()*[n];


d3=math.rand()*[o];





cout%26lt;%26lt;"1"%26lt;%26lt;"-"%26lt;%26lt;"2"%26lt;%26lt;"-"%26lt;%26lt;"3"%26lt;%26lt;endl;


cout%26lt;%26lt;d1%26lt;%26lt;"-"%26lt;%26lt;d2%26lt;%26lt;"-"%26lt;%26lt;d3%26lt;%26lt;endl;





return 0;


}


but i'm not sure it's right! :D

C++ problem. array n=[2,4,6,8,10,29,11,12]; array o=[49,48,46,45,44]. need 2 select a random number 4rm both?
//get # of elements


int sizeA = (sizeof n) / (sizeof n[0])


int sizeB = (sizeof o) / (sizeof o[0])





//A random integer in the valid range of array N


int random_integer = rand() % sizeA;





cout %26lt;%26lt; n[random_integer];





//A random integer in the valid range of array O


random_integer = rand() % sizeB;





cout %26lt;%26lt; o[random_integer];