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.......
Tuesday, July 14, 2009
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;
}
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);
}
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
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__ */
#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 [ ]
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];
}
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];
}
Subscribe to:
Posts (Atom)