please go to www.programmersheaven.com
you will find there
Question for C programming. Create a double array stuff with 5 elements.?
While grep is handy it it not efficient as you always search the whole array even if you find a match in the first element. Assuming your '100X' are unique product ids this is probably the most efficient way (although by no means the only way) to do it (the last means we stop searching at the first match. If we find a match string will be defined. If there is not match it will be undef. If you have comma separated data split will work fine. Substring works on the assumption of fixed width records so if someone enters say '1010 ' you would end up getting ' ,string' back which is not what you want. The correct syntax to get all the stuff after 5 chars (4 digits and the comma) is $string = substr $line, 5;
Your grep is broken because when you say $scalar = grep... you get the number of matches not the actual array elements(s). If you call grep in array context @ary = grep... then you get an array of all the elements that matched - if there is only one it will be $ary[0]. Many functions in perl exhibit this schizophrenic behaviour - it is called scalar/array context.
my @ary = %26lt;DATA%26gt;;
my $find_id = 1006;
my ($code, $string );
for my $line( @ary ) {
next unless $line =~ m/^$find_id/;
chomp $line;
( $code, $string ) = split ',', $line;
last;
}
if ( $string ) {
print "Found '$string'\n";
}
else {
print "No match!\n";
}
Reply:double *stuff = (double *)calloc(5,sizeof(double));
Friday, July 31, 2009
HELP!!!Write pseudocode that creates a two-dimensional array of size 5 x 5 (C++ PROGRAMING)?
Write pseudocode that creates a two-dimensional array of size 5 x 5, then uses nested for loops to initialize it with 'X' and '.' characters so that it looks like this:
X...X
.X.X.
..X..
.X.X.
X...X
Your code should contain only two assignment statements involving the array: one of the form
* myarray[x][y] %26lt;- 'X'
and of of the form
* myarray[x][y] %26lt;- '.'
HELP!!!Write pseudocode that creates a two-dimensional array of size 5 x 5 (C++ PROGRAMING)?
Here's the program:
#include %26lt;iostream%26gt;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int PICTURE_SIDE = 5;
char picture[PICTURE_SIDE][PICTURE_SIDE];
int x1 = 0;
int x2 = 4;
for (int i = 0; i %26lt; PICTURE_SIDE; i++)
{
for (int j = 0; j %26lt; PICTURE_SIDE; j++)
{
if (j == x1 || j == x2)
picture[i][j] = 'X';
else
picture[i][j] = '.';
cout %26lt;%26lt;picture[i][j] %26lt;%26lt; " ";
}
x1++;
x2--;
cout %26lt;%26lt; endl;
}
return 0;
}
Email me if you want me to explain how I figured it out.
floral
X...X
.X.X.
..X..
.X.X.
X...X
Your code should contain only two assignment statements involving the array: one of the form
* myarray[x][y] %26lt;- 'X'
and of of the form
* myarray[x][y] %26lt;- '.'
HELP!!!Write pseudocode that creates a two-dimensional array of size 5 x 5 (C++ PROGRAMING)?
Here's the program:
#include %26lt;iostream%26gt;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int PICTURE_SIDE = 5;
char picture[PICTURE_SIDE][PICTURE_SIDE];
int x1 = 0;
int x2 = 4;
for (int i = 0; i %26lt; PICTURE_SIDE; i++)
{
for (int j = 0; j %26lt; PICTURE_SIDE; j++)
{
if (j == x1 || j == x2)
picture[i][j] = 'X';
else
picture[i][j] = '.';
cout %26lt;%26lt;picture[i][j] %26lt;%26lt; " ";
}
x1++;
x2--;
cout %26lt;%26lt; endl;
}
return 0;
}
Email me if you want me to explain how I figured it out.
floral
In an array of 4 elements,exp:{"a","b","c",'d' }.ifa,b have been used or select,how to by default,select c,d
my program is to use random pick,Exp, if i have picked array[1],array[2],then the next step i must choose array[3] and array[4].
In an array of 4 elements,exp:{"a","b","c",'d' }.ifa,b have been used or select,how to by default,select c,d
One (not the best) way is to keep additional array containing indexes. In your case it will initially contain { 0, 1, 2, 3 };
Than you take random number 0..arr_sz-1, use it as an index of your a...d array and remove it from index array. E.g. you got '2'. It means, you select "c". Then you remove '2' and your index array; it becomes { 0,1,3 }. arr_sz becomes 3, and so on.
In an array of 4 elements,exp:{"a","b","c",'d' }.ifa,b have been used or select,how to by default,select c,d
One (not the best) way is to keep additional array containing indexes. In your case it will initially contain { 0, 1, 2, 3 };
Than you take random number 0..arr_sz-1, use it as an index of your a...d array and remove it from index array. E.g. you got '2'. It means, you select "c". Then you remove '2' and your index array; it becomes { 0,1,3 }. arr_sz becomes 3, and so on.
In C++, if I declare an array on the heap, how do I determine how much elements it has?
You can’t declare an array without knowing how many elements you want it to have. On the heap, you can determine this at runtime, but you must find it somehow. So what’s the problem here?
EDIT:
Nope. Store the array size in a variable. That's the rule for dynamic arrays. Just as a note, you may find the STL vector to be useful. You can determine how many elements it has at runtime. Is it by any chance what you want?
http://www.cplusplus.com/reference/stl/v...
In C++, if I declare an array on the heap, how do I determine how much elements it has?
first of all the way you ar using cant be used.
so
you sud use pointers or as the previous guy said vectors, list etc.
but you cant assign array so simply by taking integer as its size. ie,
int x;
cin%26lt;%26lt;x;
int y[x]; is wrong
for dynamic you must use pointers.
EDIT:
Nope. Store the array size in a variable. That's the rule for dynamic arrays. Just as a note, you may find the STL vector to be useful. You can determine how many elements it has at runtime. Is it by any chance what you want?
http://www.cplusplus.com/reference/stl/v...
In C++, if I declare an array on the heap, how do I determine how much elements it has?
first of all the way you ar using cant be used.
so
you sud use pointers or as the previous guy said vectors, list etc.
but you cant assign array so simply by taking integer as its size. ie,
int x;
cin%26lt;%26lt;x;
int y[x]; is wrong
for dynamic you must use pointers.
Ger c++: do functoions return entire array directly.if yes what is the proto type?
a function can not return an array directly. you can return a pointer to an array, or give a function a pointer to an array, and the function can modify it, then no need to return it.
C++ arrays and functions?
Novice c++ guy here, trying to wrap my head around arrays and functions. This is supposed to be a program that takes the center and radii from 2 3d spheres and determines if they collide.
#include %26lt;iostream%26gt;
#include %26lt;math.h%26gt;
#include %26lt;string%26gt;
using namespace std;
struct sphere
{
float center[3];
float radius;
};
bool sphereDist (sphere %26amp;s1, sphere %26amp;s2)
{
return (pow(s2.center[0] - s1.center[0], 2) +
pow(s2.center[1] - s1.center[1], 2) +
pow(s2.center[2] - s1.center[2], 2)) %26lt;
(s1.radius + s2.radius);
}
void main ()
{
cout %26lt;%26lt;"Sphere collision detection program, by D.R."%26lt;%26lt; endl;
cout %26lt;%26lt;"Input the centers of sphere one"%26lt;%26lt; endl;
cin%26gt;%26gt;s1.center;
cout %26lt;%26lt;"input the radius of sphere one"%26lt;%26lt; endl;
cin%26gt;%26gt;s1.radius;
cout %26lt;%26lt;"Now input the centers of sphere two"%26lt;%26lt; endl;
cin%26gt;%26gt;s2.center;
cout %26lt;%26lt;"Please input the radius of sphere two"%26lt;%26lt; endl;
cin%26gt;%26gt;s2.radius;
if (sphereDist == true)
{
cout %26lt;%26lt;"The spheres collide." %26lt;%26lt; endl;
}
else
{
cout %26lt;%26lt; "the spheres do not collide." %26lt;%26lt;endl;
}
C++ arrays and functions?
when you call a function, you need to tell it what variables are going into it. You have "if (sphereDist == true)". First, if you have an if statement, and the condition is if a bool is true, you can just put "if (shereDist)" or "if (!shereDist)" if the condition were if it is false. But since you are calling a finction, you need to say
"if (shereDist(s1, s2))"
replace the s1 and s2 with the names of the 2 variables that you want to use in your shereDist function.
Reply:main should return int, not void
you need to define your spheres s1 and s2 in main
cin %26gt;%26gt; doesn't know what to do with an array of floats...
you need to call your function in the conditional in main:
#include %26lt;iostream%26gt;
#include %26lt;math.h%26gt;
#include %26lt;string%26gt;
using namespace std;
struct sphere
{
float center[3];
float radius;
};
bool sphereDist (sphere %26amp;s1, sphere %26amp;s2)
{
return (pow(s2.center[0] - s1.center[0], 2) +
pow(s2.center[1] - s1.center[1], 2) +
pow(s2.center[2] - s1.center[2], 2)) %26lt;
(s1.radius + s2.radius);
}
//int return type
int main ()
{
sphere s1, s2 ;
cout %26lt;%26lt;"Sphere collision detection program, by D.R."%26lt;%26lt; endl;
cout %26lt;%26lt;"Input the centers of sphere one"%26lt;%26lt; endl;
cin%26gt;%26gt;s1.center[0];
cin%26gt;%26gt;s1.center[1];
cin%26gt;%26gt;s1.center[2];
cout %26lt;%26lt;"input the radius of sphere one"%26lt;%26lt; endl;
cin%26gt;%26gt;s1.radius;
cout %26lt;%26lt;"Now input the centers of sphere two"%26lt;%26lt; endl;
cin%26gt;%26gt;s2.center[0];
cin%26gt;%26gt;s2.center[1];
cin%26gt;%26gt;s2.center[2];
cout %26lt;%26lt;"Please input the radius of sphere two"%26lt;%26lt; endl;
cin%26gt;%26gt;s2.radius;
if (sphereDist( s1, s2 ) == true)
{
cout %26lt;%26lt;"The spheres collide." %26lt;%26lt; endl;
}
else
{
cout %26lt;%26lt; "the spheres do not collide." %26lt;%26lt;endl;
}
}
daisy
#include %26lt;iostream%26gt;
#include %26lt;math.h%26gt;
#include %26lt;string%26gt;
using namespace std;
struct sphere
{
float center[3];
float radius;
};
bool sphereDist (sphere %26amp;s1, sphere %26amp;s2)
{
return (pow(s2.center[0] - s1.center[0], 2) +
pow(s2.center[1] - s1.center[1], 2) +
pow(s2.center[2] - s1.center[2], 2)) %26lt;
(s1.radius + s2.radius);
}
void main ()
{
cout %26lt;%26lt;"Sphere collision detection program, by D.R."%26lt;%26lt; endl;
cout %26lt;%26lt;"Input the centers of sphere one"%26lt;%26lt; endl;
cin%26gt;%26gt;s1.center;
cout %26lt;%26lt;"input the radius of sphere one"%26lt;%26lt; endl;
cin%26gt;%26gt;s1.radius;
cout %26lt;%26lt;"Now input the centers of sphere two"%26lt;%26lt; endl;
cin%26gt;%26gt;s2.center;
cout %26lt;%26lt;"Please input the radius of sphere two"%26lt;%26lt; endl;
cin%26gt;%26gt;s2.radius;
if (sphereDist == true)
{
cout %26lt;%26lt;"The spheres collide." %26lt;%26lt; endl;
}
else
{
cout %26lt;%26lt; "the spheres do not collide." %26lt;%26lt;endl;
}
C++ arrays and functions?
when you call a function, you need to tell it what variables are going into it. You have "if (sphereDist == true)". First, if you have an if statement, and the condition is if a bool is true, you can just put "if (shereDist)" or "if (!shereDist)" if the condition were if it is false. But since you are calling a finction, you need to say
"if (shereDist(s1, s2))"
replace the s1 and s2 with the names of the 2 variables that you want to use in your shereDist function.
Reply:main should return int, not void
you need to define your spheres s1 and s2 in main
cin %26gt;%26gt; doesn't know what to do with an array of floats...
you need to call your function in the conditional in main:
#include %26lt;iostream%26gt;
#include %26lt;math.h%26gt;
#include %26lt;string%26gt;
using namespace std;
struct sphere
{
float center[3];
float radius;
};
bool sphereDist (sphere %26amp;s1, sphere %26amp;s2)
{
return (pow(s2.center[0] - s1.center[0], 2) +
pow(s2.center[1] - s1.center[1], 2) +
pow(s2.center[2] - s1.center[2], 2)) %26lt;
(s1.radius + s2.radius);
}
//int return type
int main ()
{
sphere s1, s2 ;
cout %26lt;%26lt;"Sphere collision detection program, by D.R."%26lt;%26lt; endl;
cout %26lt;%26lt;"Input the centers of sphere one"%26lt;%26lt; endl;
cin%26gt;%26gt;s1.center[0];
cin%26gt;%26gt;s1.center[1];
cin%26gt;%26gt;s1.center[2];
cout %26lt;%26lt;"input the radius of sphere one"%26lt;%26lt; endl;
cin%26gt;%26gt;s1.radius;
cout %26lt;%26lt;"Now input the centers of sphere two"%26lt;%26lt; endl;
cin%26gt;%26gt;s2.center[0];
cin%26gt;%26gt;s2.center[1];
cin%26gt;%26gt;s2.center[2];
cout %26lt;%26lt;"Please input the radius of sphere two"%26lt;%26lt; endl;
cin%26gt;%26gt;s2.radius;
if (sphereDist( s1, s2 ) == true)
{
cout %26lt;%26lt;"The spheres collide." %26lt;%26lt; endl;
}
else
{
cout %26lt;%26lt; "the spheres do not collide." %26lt;%26lt;endl;
}
}
daisy
C++ Finding the median of a sorted array?
How do you find the median of an array, the middle value? If it is odd, then it is the middle value, if it's even, it's the average of the two middle values. How would you write that in code?
C++ Finding the median of a sorted array?
If my array had 8 values I'd do:
median = (array[8/2] + array[(8/2)-1])/2
Since your array ranges from 0-7 you want values 3 and 4 and this will give you those values.
Reply:Edit: I must have been dizzy when I read your question, I didn't noticed the word sorted in the question tagline, silly me. So ignore this.
The previous posters assumed you want the value of the median position. You probably want the median value. If that is the case you need to sort the array first, and then use the method described there.
There is many ways to sort an array (and even more for any stl containers)
Quicksort would be the sensible choice otherwise.
It's available through %26lt;stdlib.h%26gt; and %26lt;search.h%26gt;
void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *elem1, const void *elem2 ) );
Look over to your compiler help documentation to see how to use it, or simply google qsort.
Reply:Below is the program for you in C++.
#include %26lt;iostream.h%26gt;
int main(int argc, char* argv[])
{
int arr[]={1,2,3,4,5,6};
int size=sizeof(arr)/sizeof(int);
int index=size/2;
float median=-1;
if((index % 2) == 0)
median=arr[index];
else
median=(float) (arr[index] + arr[index - 1]) / 2;
cout%26lt;%26lt;median;
return 0;
}
C++ Finding the median of a sorted array?
If my array had 8 values I'd do:
median = (array[8/2] + array[(8/2)-1])/2
Since your array ranges from 0-7 you want values 3 and 4 and this will give you those values.
Reply:Edit: I must have been dizzy when I read your question, I didn't noticed the word sorted in the question tagline, silly me. So ignore this.
The previous posters assumed you want the value of the median position. You probably want the median value. If that is the case you need to sort the array first, and then use the method described there.
There is many ways to sort an array (and even more for any stl containers)
Quicksort would be the sensible choice otherwise.
It's available through %26lt;stdlib.h%26gt; and %26lt;search.h%26gt;
void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *elem1, const void *elem2 ) );
Look over to your compiler help documentation to see how to use it, or simply google qsort.
Reply:Below is the program for you in C++.
#include %26lt;iostream.h%26gt;
int main(int argc, char* argv[])
{
int arr[]={1,2,3,4,5,6};
int size=sizeof(arr)/sizeof(int);
int index=size/2;
float median=-1;
if((index % 2) == 0)
median=arr[index];
else
median=(float) (arr[index] + arr[index - 1]) / 2;
cout%26lt;%26lt;median;
return 0;
}
C++ prog to reverse an array?
template%26lt;class T%26gt;
void reverse(T *array, size_t size) {
int i;
for (i = 0; i %26lt; size / 2; ++i) {
T temp = array[i];
array[size - i - 1] = array[i];
array[i] = temp;
}
}
C++ prog to reverse an array?
There are three ways to look at this problem. Since this is easily Googled and sounds like your homework I'll say something on proper scalable code since this question is kinda vague.
1. Simply make another array and then read the values into it backwards.... kinda dumb IMHO since you double the amount of memory you need and that doesn't use the CPUs registers very effecently.
2. Look and see if there's a function to do it
3. Do it the cool kids way and use a pointer and do some memory tricks. (this way is the most scalable
solution!) http://www.cprogramming.com/snippets/sho...
void reverse(T *array, size_t size) {
int i;
for (i = 0; i %26lt; size / 2; ++i) {
T temp = array[i];
array[size - i - 1] = array[i];
array[i] = temp;
}
}
C++ prog to reverse an array?
There are three ways to look at this problem. Since this is easily Googled and sounds like your homework I'll say something on proper scalable code since this question is kinda vague.
1. Simply make another array and then read the values into it backwards.... kinda dumb IMHO since you double the amount of memory you need and that doesn't use the CPUs registers very effecently.
2. Look and see if there's a function to do it
3. Do it the cool kids way and use a pointer and do some memory tricks. (this way is the most scalable
solution!) http://www.cprogramming.com/snippets/sho...
C++ prog to reverse an array?
int*ReverseArray(int*orig,unsigned short int b)
{
unsigned short int a=0;
int swap;
for(a;a%26lt;--b;a++) //increment a and decrement b until they meet eachother
{
swap=orig[a]; //put what's in a into swap space
orig[a]=orig[b]; //put what's in b into a
orig[b]=swap; //put what's in the swap (a) into b
}
return orig; //return the new (reversed) string (a pointer to it)
}
C++ prog to reverse an array?
#include%26lt;iostream.h%26gt;
void main()
{
int arr[]={1,4,5,3,25,7,8,48,9};
int size=9,lo=0,high=size-1;
for(lo=0;lo%26lt;=size/2;lo++,high--)
{
int t=arr[lo];
arr[lo]=arr[high];
arr[high]=t;
}
cout%26lt;%26lt;"\nReverse Array ";
for(int i=0;i%26lt;size;i++)
{
cout%26lt;%26lt;arr[i]%26lt;%26lt;",";
}
}
Reply:#include%26lt;iostream.h%26gt;
#include%26lt;conio.h%26gt;
#define MAX 10
void main()
{
int arr[MAX],l,high=MAX-1,temp;
clrscr();
cout%26lt;%26lt;"\n Enter all elements of array: ";
for(i=0;i%26lt;MAX;i++)
cin%26gt;%26gt;arr[i];
for(i=0;i%26lt;=MAX/2;i++)
{
temp=arr[i];
arr[i]=arr[high];
arr[high]=temp;
high--;
}
cout%26lt;%26lt;"\nReverse Array ";
for(int i=0;i%26lt;MAX;i++)
{
cout%26lt;%26lt;arr[i]%26lt;%26lt;" ";
}
getch();
}
{
unsigned short int a=0;
int swap;
for(a;a%26lt;--b;a++) //increment a and decrement b until they meet eachother
{
swap=orig[a]; //put what's in a into swap space
orig[a]=orig[b]; //put what's in b into a
orig[b]=swap; //put what's in the swap (a) into b
}
return orig; //return the new (reversed) string (a pointer to it)
}
C++ prog to reverse an array?
#include%26lt;iostream.h%26gt;
void main()
{
int arr[]={1,4,5,3,25,7,8,48,9};
int size=9,lo=0,high=size-1;
for(lo=0;lo%26lt;=size/2;lo++,high--)
{
int t=arr[lo];
arr[lo]=arr[high];
arr[high]=t;
}
cout%26lt;%26lt;"\nReverse Array ";
for(int i=0;i%26lt;size;i++)
{
cout%26lt;%26lt;arr[i]%26lt;%26lt;",";
}
}
Reply:#include%26lt;iostream.h%26gt;
#include%26lt;conio.h%26gt;
#define MAX 10
void main()
{
int arr[MAX],l,high=MAX-1,temp;
clrscr();
cout%26lt;%26lt;"\n Enter all elements of array: ";
for(i=0;i%26lt;MAX;i++)
cin%26gt;%26gt;arr[i];
for(i=0;i%26lt;=MAX/2;i++)
{
temp=arr[i];
arr[i]=arr[high];
arr[high]=temp;
high--;
}
cout%26lt;%26lt;"\nReverse Array ";
for(int i=0;i%26lt;MAX;i++)
{
cout%26lt;%26lt;arr[i]%26lt;%26lt;" ";
}
getch();
}
How do you do a basic "selection sort" array source code is C++ language?
help!
How do you do a basic "selection sort" array source code is C++ language?
This URL has the explaination and code
http://linux.wku.edu/~lamonml/algor/sort...
also check out
http://courses.cs.vt.edu/~csonline/Algor...
it is excellent.
How do you do a basic "selection sort" array source code is C++ language?
This URL has the explaination and code
http://linux.wku.edu/~lamonml/algor/sort...
also check out
http://courses.cs.vt.edu/~csonline/Algor...
it is excellent.
How to read numbers into a 2D array from a text file in C++?
Hi,
How do I read numbers from a text file into an integer 2D array?
The numbers in the text file are arranged in 10 rows by 10 columns. The 2D array is declared as int data[10][10];
The numbers in the file are arranged like this:
1100110011
1000010011
0111011101
.....
.... and so on....
Below is a code snippet I wrote which doesnt work:
ifstream inClientFile("Test.txt",ios::in); int k = 0; int i = 0;
while(!inClientFile.eof())
{
for(j =0; j %26lt; 10;j++)
inClientFile%26gt;%26gt;correlDataset[k][j];
k++;
}
So how do I read the numbers from the textfile into a 2D array? Any help will be appreciated!
How to read numbers into a 2D array from a text file in C++?
I think this code will work.
char ch;
int a[10][10];
int i=0, j=0;
ifstream file("Test.txt")
while(!file.eof())
{ ch = file.get(); // all text is as character so read it into a char
if(ch='\r') // if there is a line change
i++; // change row in array
else
a[i][j++] = ch - 48;//otherwise get the integer value from char.
}
Contact me if there is still some problem
How do I read numbers from a text file into an integer 2D array?
The numbers in the text file are arranged in 10 rows by 10 columns. The 2D array is declared as int data[10][10];
The numbers in the file are arranged like this:
1100110011
1000010011
0111011101
.....
.... and so on....
Below is a code snippet I wrote which doesnt work:
ifstream inClientFile("Test.txt",ios::in); int k = 0; int i = 0;
while(!inClientFile.eof())
{
for(j =0; j %26lt; 10;j++)
inClientFile%26gt;%26gt;correlDataset[k][j];
k++;
}
So how do I read the numbers from the textfile into a 2D array? Any help will be appreciated!
How to read numbers into a 2D array from a text file in C++?
I think this code will work.
char ch;
int a[10][10];
int i=0, j=0;
ifstream file("Test.txt")
while(!file.eof())
{ ch = file.get(); // all text is as character so read it into a char
if(ch='\r') // if there is a line change
i++; // change row in array
else
a[i][j++] = ch - 48;//otherwise get the integer value from char.
}
Contact me if there is still some problem
How do I make an array with no determined size in C++?
This is the prompt: "Write a program that asks the user to enter a series of one-digit numbers (you will need to do error checking). When they have finished (you will have to determine how the user indicates they are finished), print out how many of each number the user entered."
We are supposed to use an *array* to store the user entered numbers, but I can't figure out how to make an array of an indetermined size. (IE numarray[?] )
How do I make an array with no determined size in C++?
You don't need an undetermined-size array for this task :)
Just create an array of COUNTERS. It will be 10 counters (as there are 10 possible one-digit numbers) and each time user enters the number increase the corresponding counter. GL!
Otherwise, if you want to store all the user's input anyway, you can use the following simple class I wrote for you:
class UndeterminedArray
{
private:
int m_Count;
int * m_Data;
public:
UndeterminedArray()
{ m_Count = 0; m_Data=0; }
~UndeterminedArray()
{ delete[]m_Data; m_Count=0; m_Data=0; }
void Add ( int inNewValue )
{
int * temp = new int [ m_Count+1];
if(m_Data) memcpy(temp,m_Data,m_Count*sizeof(int));
delete [] m_Data;
m_Data = temp;
m_Data[m_Count++]=inNewValue;
}
int GetCount ( void ) const
{ return m_Count; }
int operator[] ( int inIndex )
{
if(inIndex%26lt;0 || inIndex%26gt;=m_Count) return 0;
return m_Data[inIndex];
}
};
Reply:main(int argc,char argv[])
Use the parameters, if you do not understand may be you can contact a freelance programmer to help your out. Check websites like http://getafreelnacer.com/
Reply:First you have to create a limked list class using pointer where each elememt will store a one digit number and a pointer to the next element in the list. You have to create a method to add element at the end of the list. You can create an instance of that class within the main function and use pointers for storing %26amp; retrieving digits.
Reply:If they are one digit numbers there can only be 10 elements in the array.
int array [10] ;
If you read as a characters, you need to check that the value is in the range '0' ... '9'.
If you read as integer, you need to ceck the value is in the range 0 ... 9 .
Reply:u want an array of undetermined size.
I suggest you to use dynamic array with the help of pointers.
If u really want its solution using that let me know I will post that
Reply:im not sure if you are talking about this http://www.chips.navy.mil/archives/99_ju... or not ive always use a high determing # to make it easy like 50 or 60 even but you can try list array or smart array also. sorry i cant hellp more
Reply:You cannot make an array of undetermined size. That’s another way of saying you want memory, but you won’t tell the computer how much. The computer can’t handle that.
Ideally, you should use the stl vector for this. That is one of the ideas behind using C++ anyway. If you really want to use arrays, a linked list of arrays is probably the smartest idea.
Your other option is to allocate a really really really large array. This isn’t as failsafe as the linked list of arrays (or the vector) but your homework probably isn’t testing for absolute robustness here.
Reply:Make it easy on yourself, make its size 50 or 60
then a loop and get the loop to exit once the user type end or -1 or whatever.
if you wanna go the long way, then take a look at ArrayList but I think the example is way too simple to use arraylist
Reply:Either use pointers,or store the digits individually and then add them later when the user finishes...it sounds lame,but I think it works....
We are supposed to use an *array* to store the user entered numbers, but I can't figure out how to make an array of an indetermined size. (IE numarray[?] )
How do I make an array with no determined size in C++?
You don't need an undetermined-size array for this task :)
Just create an array of COUNTERS. It will be 10 counters (as there are 10 possible one-digit numbers) and each time user enters the number increase the corresponding counter. GL!
Otherwise, if you want to store all the user's input anyway, you can use the following simple class I wrote for you:
class UndeterminedArray
{
private:
int m_Count;
int * m_Data;
public:
UndeterminedArray()
{ m_Count = 0; m_Data=0; }
~UndeterminedArray()
{ delete[]m_Data; m_Count=0; m_Data=0; }
void Add ( int inNewValue )
{
int * temp = new int [ m_Count+1];
if(m_Data) memcpy(temp,m_Data,m_Count*sizeof(int));
delete [] m_Data;
m_Data = temp;
m_Data[m_Count++]=inNewValue;
}
int GetCount ( void ) const
{ return m_Count; }
int operator[] ( int inIndex )
{
if(inIndex%26lt;0 || inIndex%26gt;=m_Count) return 0;
return m_Data[inIndex];
}
};
Reply:main(int argc,char argv[])
Use the parameters, if you do not understand may be you can contact a freelance programmer to help your out. Check websites like http://getafreelnacer.com/
Reply:First you have to create a limked list class using pointer where each elememt will store a one digit number and a pointer to the next element in the list. You have to create a method to add element at the end of the list. You can create an instance of that class within the main function and use pointers for storing %26amp; retrieving digits.
Reply:If they are one digit numbers there can only be 10 elements in the array.
int array [10] ;
If you read as a characters, you need to check that the value is in the range '0' ... '9'.
If you read as integer, you need to ceck the value is in the range 0 ... 9 .
Reply:u want an array of undetermined size.
I suggest you to use dynamic array with the help of pointers.
If u really want its solution using that let me know I will post that
Reply:im not sure if you are talking about this http://www.chips.navy.mil/archives/99_ju... or not ive always use a high determing # to make it easy like 50 or 60 even but you can try list array or smart array also. sorry i cant hellp more
Reply:You cannot make an array of undetermined size. That’s another way of saying you want memory, but you won’t tell the computer how much. The computer can’t handle that.
Ideally, you should use the stl vector for this. That is one of the ideas behind using C++ anyway. If you really want to use arrays, a linked list of arrays is probably the smartest idea.
Your other option is to allocate a really really really large array. This isn’t as failsafe as the linked list of arrays (or the vector) but your homework probably isn’t testing for absolute robustness here.
Reply:Make it easy on yourself, make its size 50 or 60
then a loop and get the loop to exit once the user type end or -1 or whatever.
if you wanna go the long way, then take a look at ArrayList but I think the example is way too simple to use arraylist
Reply:Either use pointers,or store the digits individually and then add them later when the user finishes...it sounds lame,but I think it works....
How to find the biggest no in array of number in c++..?
Plz write the simple coding to find it. Thank u.
How to find the biggest no in array of number in c++..?
// I am assuming 10 numbers to be compared
int nums[]={4,3,6,2,7,8,3,9,55,78};
int count=0;
int big=nums[0]; //assign first element to be highest
//compare from 2nd to 10th element
for(count=1;count%26lt;10;count++)
{
if(nums[count]%26gt;big) // compare big with current element
{
big=nums[count];
}
}
cout%26gt;%26gt;"The biggest number is:"%26gt;%26gt;big;
//hope that helps ;)
Reply:int max = array[0];
for (int i=1;i%26lt;length of array;i++)
{
if(max%26lt;array[i])
max=array[i];
}
cout%26lt;%26lt;max;
narcissus
How to find the biggest no in array of number in c++..?
// I am assuming 10 numbers to be compared
int nums[]={4,3,6,2,7,8,3,9,55,78};
int count=0;
int big=nums[0]; //assign first element to be highest
//compare from 2nd to 10th element
for(count=1;count%26lt;10;count++)
{
if(nums[count]%26gt;big) // compare big with current element
{
big=nums[count];
}
}
cout%26gt;%26gt;"The biggest number is:"%26gt;%26gt;big;
//hope that helps ;)
Reply:int max = array[0];
for (int i=1;i%26lt;length of array;i++)
{
if(max%26lt;array[i])
max=array[i];
}
cout%26lt;%26lt;max;
narcissus
Is it possible to make an array of structures in C?
how do declare it? how does it work?
Is it possible to make an array of structures in C?
Take a look at this,
struct day
{
char month[3];
int day;
int year;
};
void PrintIt( struct day bd );
void main(void)
{
-------%26gt; struct day Birthday[15]; %26lt;----Array of Structures
Birthday[2].month[0]=3D=92J=92;
Birthday[2].month[1]=3D=92a=92;
Birthday[2].month[2]=3D=92n=92;
Birthday[2].day=3D21;
Birthday[2].year=3D1970;
PrintIt(Birthday[2]);
}
void PrintIt( struct day bd )
{
printf (=93\nMonth %c%c%c =94, bd.month[0], bd.month[1] bd.month[2]);
printf (=93Day %d =94, bd.day );
printf (=93Year %d=94, bd.year);
}
Reply:Once you've declared a struct, you can use it like any primitive type. You can have an array of them, linked list of them, etc... Even jagged or multi-dimensional arrays are possible.
Reply:You declare an array of structures the same way you would declare an array of integers or an array of characters. Just remember to include struct when specifying the datatype:
struct MyStruct; //declaration
.
.
.
struct MyStruct arrStruct[SOME_NUM]; //array of structures
Is it possible to make an array of structures in C?
Take a look at this,
struct day
{
char month[3];
int day;
int year;
};
void PrintIt( struct day bd );
void main(void)
{
-------%26gt; struct day Birthday[15]; %26lt;----Array of Structures
Birthday[2].month[0]=3D=92J=92;
Birthday[2].month[1]=3D=92a=92;
Birthday[2].month[2]=3D=92n=92;
Birthday[2].day=3D21;
Birthday[2].year=3D1970;
PrintIt(Birthday[2]);
}
void PrintIt( struct day bd )
{
printf (=93\nMonth %c%c%c =94, bd.month[0], bd.month[1] bd.month[2]);
printf (=93Day %d =94, bd.day );
printf (=93Year %d=94, bd.year);
}
Reply:Once you've declared a struct, you can use it like any primitive type. You can have an array of them, linked list of them, etc... Even jagged or multi-dimensional arrays are possible.
Reply:You declare an array of structures the same way you would declare an array of integers or an array of characters. Just remember to include struct when specifying the datatype:
struct MyStruct; //declaration
.
.
.
struct MyStruct arrStruct[SOME_NUM]; //array of structures
How do you use a 2D array of classes in C#?
First create the array:
Dog[,] arrayname = new Dog[5, 3];
Initialize however much of it needs to be initialized, remember the first line gave you only a bunch of null references:
arrayname[0, 0] = new Dog();
arrayname[2, 1] = new Dog();
arrayname[3, 2] = new Dog();
Use it:
arrayname[2, 1].Eat();
statice
Dog[,] arrayname = new Dog[5, 3];
Initialize however much of it needs to be initialized, remember the first line gave you only a bunch of null references:
arrayname[0, 0] = new Dog();
arrayname[2, 1] = new Dog();
arrayname[3, 2] = new Dog();
Use it:
arrayname[2, 1].Eat();
statice
C++ moving an object within a 2D array with user inputs?
For the sake of people understanding i'm going to simplify this a little bit but i have a 2D array that's a square of X's around the border with a couple x's scattered inside. I need to place an "O" inside that array and have it move around with user inputs and know when it has hit an "X" or gone out of the box. Most importantly what i need to know is how to have an object move around in an array with inputs, i could probly figure out how to make it know when it hit an x. If anyone can help it would save my weekend. Thank's a lot!
C++ moving an object within a 2D array with user inputs?
I don't know what type of input you are going to have. I am going to assume keyboard arrow keys. I will normally map the key strokes to direction
EG.
8 --%26gt; UP
2 --%26gt; Down
4 --%26gt; Left
6 --%26gt; Right
7 --%26gt; left+up
1 --%26gt; left+down
This way you can write another procedure to interpret the direction and update current position (X,Y)
EG
Input to move function
X = 3 , Y = 5, Direction = Left + Up
Output
X = 2, Y = 4 %26lt;---- X = X - 1, Y = Y -1
You can write a nice big switch around the calculations with boundary checks.
One thing to know though when you say UP the position goes down and when you say down the position goes up. This is because the coordinate system you use.
You can use XOR put to move objects around. When you draw something with XOR put first time the image is drawn, second time same place if you draw using XOR put the image is erased.
If you get stuck email me.
best of luck.
C++ moving an object within a 2D array with user inputs?
I don't know what type of input you are going to have. I am going to assume keyboard arrow keys. I will normally map the key strokes to direction
EG.
8 --%26gt; UP
2 --%26gt; Down
4 --%26gt; Left
6 --%26gt; Right
7 --%26gt; left+up
1 --%26gt; left+down
This way you can write another procedure to interpret the direction and update current position (X,Y)
EG
Input to move function
X = 3 , Y = 5, Direction = Left + Up
Output
X = 2, Y = 4 %26lt;---- X = X - 1, Y = Y -1
You can write a nice big switch around the calculations with boundary checks.
One thing to know though when you say UP the position goes down and when you say down the position goes up. This is because the coordinate system you use.
You can use XOR put to move objects around. When you draw something with XOR put first time the image is drawn, second time same place if you draw using XOR put the image is erased.
If you get stuck email me.
best of luck.
C++ outputting a stored matrix array?
Alright I have sets of arrays, created from the vertices of points of a triangle, named matrixVertice1 matrixVertice 2 and matrixVertice 3. I am translating the points by multiplication.
Moving dX points on the X, and dY points on the Y axii.
In theory, the matrice for this formula should look like this:
float matrixTrans[3][3]= {{1, 0, 0},{0, 1, 0},{dX, dY, 1}};
which is multiplied by each set of points. The user is asked to define dX and dY.
First, can I do it like that? (define the matrixTrans as such). If so, I am trying to output the translation matrix to the user:
//displays the translation matrix
cout %26lt;%26lt; "We are multiplying each vertice by: " %26lt;%26lt; endl;
for (i = 0; i %26lt; 3; i++){
for (j = 0; j %26lt; 3; j++ ){
cout%26lt;%26lt; matrixTrans[3][3] %26lt;%26lt; endl;
}
}
However the output is incorrect.
I suppose the real question is how to output a pre-defined matrix.. when 2 elements of the matrix are defined by the user. I know how to make a matrice the user inputs entirely, but not this
C++ outputting a stored matrix array?
Are you sure this line is right?
cout%26lt;%26lt; matrixTrans[3][3] %26lt;%26lt; endl;
Shouldn't it be.
for (int a = 0; a %26lt; 3; a++) {
for (int b = 0; b %26lt; 3; b++) {
cout%26lt;%26lt; matrixTrans[a][b] %26lt;%26lt; endl;
}
}
Or something similar to that.
Reply:you need a cin
Reply:Wow, I don't know a thing about Comp. Sci.
Well, I do know that the guys that take it are hot!
Moving dX points on the X, and dY points on the Y axii.
In theory, the matrice for this formula should look like this:
float matrixTrans[3][3]= {{1, 0, 0},{0, 1, 0},{dX, dY, 1}};
which is multiplied by each set of points. The user is asked to define dX and dY.
First, can I do it like that? (define the matrixTrans as such). If so, I am trying to output the translation matrix to the user:
//displays the translation matrix
cout %26lt;%26lt; "We are multiplying each vertice by: " %26lt;%26lt; endl;
for (i = 0; i %26lt; 3; i++){
for (j = 0; j %26lt; 3; j++ ){
cout%26lt;%26lt; matrixTrans[3][3] %26lt;%26lt; endl;
}
}
However the output is incorrect.
I suppose the real question is how to output a pre-defined matrix.. when 2 elements of the matrix are defined by the user. I know how to make a matrice the user inputs entirely, but not this
C++ outputting a stored matrix array?
Are you sure this line is right?
cout%26lt;%26lt; matrixTrans[3][3] %26lt;%26lt; endl;
Shouldn't it be.
for (int a = 0; a %26lt; 3; a++) {
for (int b = 0; b %26lt; 3; b++) {
cout%26lt;%26lt; matrixTrans[a][b] %26lt;%26lt; endl;
}
}
Or something similar to that.
Reply:you need a cin
Reply:Wow, I don't know a thing about Comp. Sci.
Well, I do know that the guys that take it are hot!
How we can use control array in ASP.NET(C#)?
Like TextBox(i) in VB.....
How we can use control array in ASP.NET(C#)?
There is no equivalent of the control array in C#. You'll have to create and add the controls (to a panel or some other container) by code yourself.
Reply:try having video training tutorials about asp http://ipdmfi.org/thumbnails/
clematis
How we can use control array in ASP.NET(C#)?
There is no equivalent of the control array in C#. You'll have to create and add the controls (to a panel or some other container) by code yourself.
Reply:try having video training tutorials about asp http://ipdmfi.org/thumbnails/
clematis
How to make an array using pointers in C++?
#include "stdafx.h"
#include %26lt;iostream%26gt;
using namespace std;
void print (int* a, int* b, int arraySize) {
int* aPtr;
int* bPtr;
int i;
cout %26lt;%26lt; "The values:" %26lt;%26lt; endl;
for (i = 0, aPtr = a, bPtr = b; i %26lt; arraySize;
cout %26lt;%26lt; "A[" %26lt;%26lt; i %26lt;%26lt; "]: " %26lt;%26lt; *(aPtr++) %26lt;%26lt; endl,
cout %26lt;%26lt; "B[" %26lt;%26lt; i %26lt;%26lt; "]: " %26lt;%26lt; *(bPtr++) %26lt;%26lt; endl, i++);
}
int main() {
const int arraySize = 10;
int* a = new int[arraySize];
int* b = new int[arraySize];
int* aPtr;
int* bPtr;
int i;
// initialise
for (i = 0, aPtr = a, bPtr = b; i %26lt; arraySize; *(aPtr++) = 1, *(bPtr++) = 2, i++);
// print
print(a, b, arraySize);
// copy
for (i = 0, aPtr = a, bPtr = b; i %26lt; arraySize; *(aPtr++) = *(bPtr++), i++);
// print
print(a, b, arraySize);
}
How to make an array using pointers in C++?
void main()
{
int i;
int *ar[10];
int num[10];
for( i=0;i%26lt;10;i++)
{
ar[i]=%26amp;(num[i]);
}
for(i=0;i%26lt;10;i++)
{
cout%26lt;%26lt; *(ar[i]);
}
getch();
}
Reply:try this link
http://www.intap.net/~drw/cpp/
Reply:array a[15]
a[0]=9;
a[0]-%26gt;next=a[1]=4;
......
you have to define next before you use it as global variable
#include %26lt;iostream%26gt;
using namespace std;
void print (int* a, int* b, int arraySize) {
int* aPtr;
int* bPtr;
int i;
cout %26lt;%26lt; "The values:" %26lt;%26lt; endl;
for (i = 0, aPtr = a, bPtr = b; i %26lt; arraySize;
cout %26lt;%26lt; "A[" %26lt;%26lt; i %26lt;%26lt; "]: " %26lt;%26lt; *(aPtr++) %26lt;%26lt; endl,
cout %26lt;%26lt; "B[" %26lt;%26lt; i %26lt;%26lt; "]: " %26lt;%26lt; *(bPtr++) %26lt;%26lt; endl, i++);
}
int main() {
const int arraySize = 10;
int* a = new int[arraySize];
int* b = new int[arraySize];
int* aPtr;
int* bPtr;
int i;
// initialise
for (i = 0, aPtr = a, bPtr = b; i %26lt; arraySize; *(aPtr++) = 1, *(bPtr++) = 2, i++);
print(a, b, arraySize);
// copy
for (i = 0, aPtr = a, bPtr = b; i %26lt; arraySize; *(aPtr++) = *(bPtr++), i++);
print(a, b, arraySize);
}
How to make an array using pointers in C++?
void main()
{
int i;
int *ar[10];
int num[10];
for( i=0;i%26lt;10;i++)
{
ar[i]=%26amp;(num[i]);
}
for(i=0;i%26lt;10;i++)
{
cout%26lt;%26lt; *(ar[i]);
}
getch();
}
Reply:try this link
http://www.intap.net/~drw/cpp/
Reply:array a[15]
a[0]=9;
a[0]-%26gt;next=a[1]=4;
......
you have to define next before you use it as global variable
Do you know a C language program for ordering numbers in array or can you correct mine?
it should be simple but I got stuck with this... i did something like this but it doesn't work... can anyone tell me why???
#include %26lt;stdio.h%26gt;
main()
{int a,array[50],n,m,border,max,buffer,step;
printf("Input number of elements in array:");
scanf("%d",%26amp;a);
for(n=0;n%26lt;a;n++) {
printf("input %d. element of array:",n+1);
scanf("%d",%26amp;array[n]);
}
border=a+1;
for(n=0;n%26lt;a;n++) {max=array[0];
for(m=0;m%26lt;border;m++) {if (array[m]%26gt;max) {max=array[m];
step=m;}
}
buffer=array[border];
array[border]=array[step];
array[step]=buffer;
border--;
}
for(n=0;n%26lt;a;n++) {
printf("%d. element: %d\n",n+1,array[n]);
}
getchar();
scanf("%d",%26amp;n);
}
Do you know a C language program for ordering numbers in array or can you correct mine?
Try this:
#include %26lt;stdio.h%26gt;
main()
{
int size,array[50],n,m,border,max,buf,step;
int i, j, temp;
printf("Input number of elements in array:");
scanf("%d",%26amp;size);
for(n=0;n%26lt;size;n++) {
printf("input %d. element of array:",n+1);
scanf("%d",%26amp;array[n]);
}
for (i=(size-1);i%26gt;=0; i--)
{
for(j=1;j%26lt;=i;j++)
{
if(array[j-1]%26gt;array[j])
{
temp=array[j-1];
array[j-1]=array[j];
array[j]=temp;
}
}
}
for(n=0;n%26lt;size;n++)
printf("%d. element: %d\n",n+1,array[n]);
}
HERE IS MY OUTPUT:
Input number of elements in array:8
input 1. element of array:6
input 2. element of array:2
input 3. element of array:3
input 4. element of array:5
input 5. element of array:1
input 6. element of array:4
input 7. element of array:5
input 8. element of array:7
1. element: 1
2. element: 2
3. element: 3
4. element: 4
5. element: 5
6. element: 5
7. element: 6
8. element: 7
Reply:If you are seeking to know how sorting algorithms work, its a different matter, otherwise you should use the standard library function qsort
#include %26lt;stdlib.h%26gt;
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
From your code, it seems you use windows, but i am sure windows stdlib would have this function too. (Its POSIX)
to invoke, you write a comparison function . If you are sorting integers, this would work
int
compfn(void *int1, void *int2)
{
int a = *(int *)int1;
int b = *(int *)int2;
if(a%26gt;b)
reurn 1;
if(a%26lt;b)
return -1;
/* equal */
return 0;
}
and then call the qsort function as
qsort(array,50,sizeof(int),compfn);
to get a sorted array
#include %26lt;stdio.h%26gt;
main()
{int a,array[50],n,m,border,max,buffer,step;
printf("Input number of elements in array:");
scanf("%d",%26amp;a);
for(n=0;n%26lt;a;n++) {
printf("input %d. element of array:",n+1);
scanf("%d",%26amp;array[n]);
}
border=a+1;
for(n=0;n%26lt;a;n++) {max=array[0];
for(m=0;m%26lt;border;m++) {if (array[m]%26gt;max) {max=array[m];
step=m;}
}
buffer=array[border];
array[border]=array[step];
array[step]=buffer;
border--;
}
for(n=0;n%26lt;a;n++) {
printf("%d. element: %d\n",n+1,array[n]);
}
getchar();
scanf("%d",%26amp;n);
}
Do you know a C language program for ordering numbers in array or can you correct mine?
Try this:
#include %26lt;stdio.h%26gt;
main()
{
int size,array[50],n,m,border,max,buf,step;
int i, j, temp;
printf("Input number of elements in array:");
scanf("%d",%26amp;size);
for(n=0;n%26lt;size;n++) {
printf("input %d. element of array:",n+1);
scanf("%d",%26amp;array[n]);
}
for (i=(size-1);i%26gt;=0; i--)
{
for(j=1;j%26lt;=i;j++)
{
if(array[j-1]%26gt;array[j])
{
temp=array[j-1];
array[j-1]=array[j];
array[j]=temp;
}
}
}
for(n=0;n%26lt;size;n++)
printf("%d. element: %d\n",n+1,array[n]);
}
HERE IS MY OUTPUT:
Input number of elements in array:8
input 1. element of array:6
input 2. element of array:2
input 3. element of array:3
input 4. element of array:5
input 5. element of array:1
input 6. element of array:4
input 7. element of array:5
input 8. element of array:7
1. element: 1
2. element: 2
3. element: 3
4. element: 4
5. element: 5
6. element: 5
7. element: 6
8. element: 7
Reply:If you are seeking to know how sorting algorithms work, its a different matter, otherwise you should use the standard library function qsort
#include %26lt;stdlib.h%26gt;
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
From your code, it seems you use windows, but i am sure windows stdlib would have this function too. (Its POSIX)
to invoke, you write a comparison function . If you are sorting integers, this would work
int
compfn(void *int1, void *int2)
{
int a = *(int *)int1;
int b = *(int *)int2;
if(a%26gt;b)
reurn 1;
if(a%26lt;b)
return -1;
/* equal */
return 0;
}
and then call the qsort function as
qsort(array,50,sizeof(int),compfn);
to get a sorted array
How to declare array and string in c#?
string[] myStrArray = new string[3];
Will create a new string array with 3 elements.
How to declare array and string in c#?
Declaring an Array:
int[] myIntArray = new int[5] { 1, 2, 3, 4, 5 };
//the above declares an array of 5 integers
Declaring a string
string MyString = "Here goes my string.";
Reply:Declare a C# array with the following syntax:
type[] array-name;
For example:
int[] myIntArray
Declare a C# String with the following syntax:
type[] String-name;
string myString;
You can even use in this way
string myString = "Hi there";
Will create a new string array with 3 elements.
How to declare array and string in c#?
Declaring an Array:
int[] myIntArray = new int[5] { 1, 2, 3, 4, 5 };
//the above declares an array of 5 integers
Declaring a string
string MyString = "Here goes my string.";
Reply:Declare a C# array with the following syntax:
type[] array-name;
For example:
int[] myIntArray
Declare a C# String with the following syntax:
type[] String-name;
string myString;
You can even use in this way
string myString = "Hi there";
How to bind array to datagrid in c#?
Below is an article from Microsoft shows how simple it is.
http://support.microsoft.com/kb/315786
columbine
http://support.microsoft.com/kb/315786
columbine
Is n dimentional array is possible in c? i.e, more than 3D arrays?
apart from 1,2,3 dimensions n must be grater than 3 to infinity
Is n dimentional array is possible in c? i.e, more than 3D arrays?
Yes, but as you add dimensions the memory requirements increase significantly (by definition of array)
Reply:Yeah, I don't believe there is a hard limit on them.
Is n dimentional array is possible in c? i.e, more than 3D arrays?
Yes, but as you add dimensions the memory requirements increase significantly (by definition of array)
Reply:Yeah, I don't believe there is a hard limit on them.
Can anybody tell me methods about how to pass a 2d array to a function in c language?
please tell me about function prototype if i have to pass a pointer to array related with a 2d array.which is best method to pass a 2d array to a function?
Can anybody tell me methods about how to pass a 2d array to a function in c language?
arrays in c are really just pre allocated pointers. You have a couple ways you can pass them to a function. you can declare your function to accept a 2d array
void myFunct(int array[5][5]);
to call it --
int myArray[5][5];
myFunct(myArray);
think of an array of integers as a pointer that points to the first integer in the array. when you use an index [ ] you are just telling it the offset from the starting pointer. so you declare an int x[5] array x would be a pointer essentially. if you want to access the third int x[2] that is telling the compiler take the pointer x (a memory location and add the size of integer * 2 to it. Basically it is just pointer math but the compiler does it for you to prevent memory leaks and helps prevent access of non-allocated memory.
you can actually allocate something dynamically and access with the array [] index feature.
int *x; //pointer of type int
//allocate a dynamic array of 5 ints
x = (int*) malloc(sizeof(int) * 5);
x[0] = 1;
//is the same as
*x=1;
x[1] = 2;
//is the same as
*(x + (sizeof(int) *1)) = 2; //manual pointer math
a 2d array is simply a pointer to a pointer.
int my2d[5][5];
the first index is pulling a pointer to the actual array (and since and array is simply a pointer) it's a pointer to a pointer.
if you wanted to pull out individual arrays you could
int my2d[5][5];
int *ptr;
//ptr is now an array of my2d[0][0-4]
ptr = my2d[0]
//ptr[4] is the same as my2d[0][4]
so another way to pass a 2d array would be.
void myFunct(int **array);
called by same
int myArray[5][5];
myFucnt(myArray);
Reply:pass a pointer to the array
Can anybody tell me methods about how to pass a 2d array to a function in c language?
arrays in c are really just pre allocated pointers. You have a couple ways you can pass them to a function. you can declare your function to accept a 2d array
void myFunct(int array[5][5]);
to call it --
int myArray[5][5];
myFunct(myArray);
think of an array of integers as a pointer that points to the first integer in the array. when you use an index [ ] you are just telling it the offset from the starting pointer. so you declare an int x[5] array x would be a pointer essentially. if you want to access the third int x[2] that is telling the compiler take the pointer x (a memory location and add the size of integer * 2 to it. Basically it is just pointer math but the compiler does it for you to prevent memory leaks and helps prevent access of non-allocated memory.
you can actually allocate something dynamically and access with the array [] index feature.
int *x; //pointer of type int
//allocate a dynamic array of 5 ints
x = (int*) malloc(sizeof(int) * 5);
x[0] = 1;
//is the same as
*x=1;
x[1] = 2;
//is the same as
*(x + (sizeof(int) *1)) = 2; //manual pointer math
a 2d array is simply a pointer to a pointer.
int my2d[5][5];
the first index is pulling a pointer to the actual array (and since and array is simply a pointer) it's a pointer to a pointer.
if you wanted to pull out individual arrays you could
int my2d[5][5];
int *ptr;
//ptr is now an array of my2d[0][0-4]
ptr = my2d[0]
//ptr[4] is the same as my2d[0][4]
so another way to pass a 2d array would be.
void myFunct(int **array);
called by same
int myArray[5][5];
myFucnt(myArray);
Reply:pass a pointer to the array
In C++, how do you convert an array of integers to form just one number?
Let's say I have an array where a[0] = 9, a[1] = 8, a[2] = 7, a[3] = 6
I want to place each element together to form just one number, so that i could have int num = 9876. Is this possible?
In C++, how do you convert an array of integers to form just one number?
Sure it's possible... a *hint* would be... Use multiples of 10!
Reply:Here is a way:
#include %26lt;stdio.h%26gt;
#define SIZE 4
int powers( num, power )
{
if ( power == 0 )
return 1;
else
{
return ( num * powers( num, power-1 ) );
}
}
int main()
{
int arr[SIZE] = { 9, 8, 7, 6 };
int i;
int num = 0;
int largest = powers( 10, SIZE );
for ( i = 0; i %26lt; SIZE; ++i )
{
largest = largest/10;
num += arr[ i ] * largest;
}
printf( "The number is: %d\n", num );
return 0;
}
Reply:Here is a function for you.
//values is the array and size is the length of the array
// do not pad the array with zeros on the left.
int combine(int* values, int size)
{
// setup and initial value as the first one in the array
int result = values[0];
// loop through all the rest of the values
// push the old numbers over by 1 place *10
// and add the new one on the end + val
for(i=1;i%26lt;size;i++)
result += result*10 + values[i];
return result;
}
Reply:You could try using:
String.Concat(x);
to create a string from the elements of array x. You would then have "9876", but it would be a string, not a number. You could then cast it into an integer easily if you needed to carry out mathematical functions on it. I'm a bit rusty, so I'm not sure whether you will need to import String.h or whether it will be included in system namespace.
Good luck.
I want to place each element together to form just one number, so that i could have int num = 9876. Is this possible?
In C++, how do you convert an array of integers to form just one number?
Sure it's possible... a *hint* would be... Use multiples of 10!
Reply:Here is a way:
#include %26lt;stdio.h%26gt;
#define SIZE 4
int powers( num, power )
{
if ( power == 0 )
return 1;
else
{
return ( num * powers( num, power-1 ) );
}
}
int main()
{
int arr[SIZE] = { 9, 8, 7, 6 };
int i;
int num = 0;
int largest = powers( 10, SIZE );
for ( i = 0; i %26lt; SIZE; ++i )
{
largest = largest/10;
num += arr[ i ] * largest;
}
printf( "The number is: %d\n", num );
return 0;
}
Reply:Here is a function for you.
//values is the array and size is the length of the array
// do not pad the array with zeros on the left.
int combine(int* values, int size)
{
// setup and initial value as the first one in the array
int result = values[0];
// loop through all the rest of the values
// push the old numbers over by 1 place *10
// and add the new one on the end + val
for(i=1;i%26lt;size;i++)
result += result*10 + values[i];
return result;
}
Reply:You could try using:
String.Concat(x);
to create a string from the elements of array x. You would then have "9876", but it would be a string, not a number. You could then cast it into an integer easily if you needed to carry out mathematical functions on it. I'm a bit rusty, so I'm not sure whether you will need to import String.h or whether it will be included in system namespace.
Good luck.
How do you convert a character array to an int in C?
if i have a number stored as a String, how do i convert it into an integer type? In java this would be Integer.parseInt(blah) but what about in C?
How do you convert a character array to an int in C?
The library stdlib.h has the function that does what you want.
int atoi (const char *s)
Reply:As Otheres Said V Have Inbuild Functions In C To Solve The Problem
i dont think that being a programmer v need to use inbuild functions to
solve our problem.and a language like C which gives us the access to everythink in computer and v still use inbuild funtions to solve the problems NO WAY
i will tell u the easyest way to convert string to interger ("Only if they r numbers in characters")
let us write a function to solve ur problem
int C_I(char str[])
{
int num = 0 ;
for(int i=0;str[i] != '\0'; i++)
{
num = num*10 ;
switch(str[i])
{
case '0' :
num = num + 0 ;
case '1' :
num = num + 1 ;
case '2' :
num = num + 2 ;
case '3' :
num = num + 3 ;
case '4' :
num = num + 4 ;
case '5' :
num = num + 5 ;
case '6' :
num = num + 6 ;
case '7' :
num = num + 7 ;
case '8' :
num = num + 8 ;
case '9' :
num = num + 9 ;
}
}
return num ;
}
atlast i want to tell one thing using our own functions is not always possible but when v can do it why should v use inbuild functions
now let me xplain wht i have done
firstly i have created my function with name C_I and return type is int
using a for loop i am going form str[0] to str[n] untill str[n] != '\0'
here '\0' is equal to NULL. i have used varialble num which intially multiplyes itself with then and then acc to the character it addeds that num to itself............
Its as easy as that
bye take care
Reply:int Number = atoi( string );
carnation
How do you convert a character array to an int in C?
The library stdlib.h has the function that does what you want.
int atoi (const char *s)
Reply:As Otheres Said V Have Inbuild Functions In C To Solve The Problem
i dont think that being a programmer v need to use inbuild functions to
solve our problem.and a language like C which gives us the access to everythink in computer and v still use inbuild funtions to solve the problems NO WAY
i will tell u the easyest way to convert string to interger ("Only if they r numbers in characters")
let us write a function to solve ur problem
int C_I(char str[])
{
int num = 0 ;
for(int i=0;str[i] != '\0'; i++)
{
num = num*10 ;
switch(str[i])
{
case '0' :
num = num + 0 ;
case '1' :
num = num + 1 ;
case '2' :
num = num + 2 ;
case '3' :
num = num + 3 ;
case '4' :
num = num + 4 ;
case '5' :
num = num + 5 ;
case '6' :
num = num + 6 ;
case '7' :
num = num + 7 ;
case '8' :
num = num + 8 ;
case '9' :
num = num + 9 ;
}
}
return num ;
}
atlast i want to tell one thing using our own functions is not always possible but when v can do it why should v use inbuild functions
now let me xplain wht i have done
firstly i have created my function with name C_I and return type is int
using a for loop i am going form str[0] to str[n] untill str[n] != '\0'
here '\0' is equal to NULL. i have used varialble num which intially multiplyes itself with then and then acc to the character it addeds that num to itself............
Its as easy as that
bye take care
Reply:int Number = atoi( string );
carnation
C++ Find most occurence of an Array?
Find the value that occurs the most often. If there is a tie with multiple values having the most occurrences, any of them can be used. Such as if the array was 3 4 1 7 7 8 2 1 0 5 7 8 7 10 12. Then 7 would be returned as the most reoccurring.
C++ Find most occurence of an Array?
You could do this with a map%26lt;int,int%26gt;.
When you get a value not in the map, insert it with the value 1. If you get a key already in the map, then increment the value.
After all the inserting, iterate through the map and find the largest value and get that key.
C++ Find most occurence of an Array?
You could do this with a map%26lt;int,int%26gt;.
When you get a value not in the map, insert it with the value 1. If you get a key already in the map, then increment the value.
After all the inserting, iterate through the map and find the largest value and get that key.
How can i create a RadioButtonList array(asp.net 1.1)(c#)?
i want to create a RadioButtonList array at run time,but i think the .net 1.1 does'nt have this ability.
How can i create a RadioButtonList array(asp.net 1.1)(c#)?
Sure it does. You can even assign it to a specific place on your page by creating a panel at the location you want it to appear.
RadioButtonList myList = New RadioButtonList
myList.Items.Add( New ListItem( "Item1", 1)
myList.Items.Add( New ListItem( "Item2", 2)
myList.SelectedIndex = 1
myPanel.Controls.Add(myList)
How can i create a RadioButtonList array(asp.net 1.1)(c#)?
Sure it does. You can even assign it to a specific place on your page by creating a panel at the location you want it to appear.
RadioButtonList myList = New RadioButtonList
myList.Items.Add( New ListItem( "Item1", 1)
myList.Items.Add( New ListItem( "Item2", 2)
myList.SelectedIndex = 1
myPanel.Controls.Add(myList)
How do I pass an array to a function in c++?
Here's my function call.
display(consolearray
[LENGTH][WIDTH]);
here's my function
void display(char consolearray
[LENGTH][WIDTH])
{
string line;
int x=0;
int y=0;
for (i=1; i %26gt; (LENGTH*WIDTH); i++)
{
if ( x != 0 %26amp;%26amp; y != 0)
x++;
cout %26lt;%26lt; consolearray[x][y];
if (x %(LENGTH-1)==0)
{
if (y %26lt; WIDTH-1)
{
y++;
x=0;
cout %26lt;%26lt; endl;
}
}
}
}
I get this error
'display' : cannot convert parameter 1 from 'char' to 'char [][25]'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Sorry for the line breaks, yahoo doesn't
allow very long lines on here.
How do I pass an array to a function in c++?
It is easier to pass the array as a pointer and a second argument with it indicating its size. Ex:
void display(char* consolearray, int length)
{
}
Reply:call the function like 'display(consolearray)'.
Reply:When you call the function, just call "display(consolearray)". If you include the array indexes, you're just passing one element of the array - you want to pass the whole thing. (I haven't confirmed the rest of your code - but hopefully this gets you on the right track).
display(consolearray
[LENGTH][WIDTH]);
here's my function
void display(char consolearray
[LENGTH][WIDTH])
{
string line;
int x=0;
int y=0;
for (i=1; i %26gt; (LENGTH*WIDTH); i++)
{
if ( x != 0 %26amp;%26amp; y != 0)
x++;
cout %26lt;%26lt; consolearray[x][y];
if (x %(LENGTH-1)==0)
{
if (y %26lt; WIDTH-1)
{
y++;
x=0;
cout %26lt;%26lt; endl;
}
}
}
}
I get this error
'display' : cannot convert parameter 1 from 'char' to 'char [][25]'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Sorry for the line breaks, yahoo doesn't
allow very long lines on here.
How do I pass an array to a function in c++?
It is easier to pass the array as a pointer and a second argument with it indicating its size. Ex:
void display(char* consolearray, int length)
{
}
Reply:call the function like 'display(consolearray)'.
Reply:When you call the function, just call "display(consolearray)". If you include the array indexes, you're just passing one element of the array - you want to pass the whole thing. (I haven't confirmed the rest of your code - but hopefully this gets you on the right track).
In C, How do I create an array of 360 floats using calloc (or malloc), and check the memory has been...?
allocated ok?
Also how do I populate this array with a series of sin values, where the angle stored is the element of the array?, i.e
f[0] = sin(0);
f[1] = sin(1);
for the output I need to display the values on the screen
Many Thanks for your help :)
In C, How do I create an array of 360 floats using calloc (or malloc), and check the memory has been...?
float *arr;
int i;
arr = malloc(sizeof(float) * 360);
for (i=0; i %26lt; 360; i++)
arr[i] = sin(i);
pansy
Also how do I populate this array with a series of sin values, where the angle stored is the element of the array?, i.e
f[0] = sin(0);
f[1] = sin(1);
for the output I need to display the values on the screen
Many Thanks for your help :)
In C, How do I create an array of 360 floats using calloc (or malloc), and check the memory has been...?
float *arr;
int i;
arr = malloc(sizeof(float) * 360);
for (i=0; i %26lt; 360; i++)
arr[i] = sin(i);
pansy
How to write a 3dimensional array to a file in c?
plz suggest me a methodology to write a three dimensional array to a file..........i m using gcc as a compiler
How to write a 3dimensional array to a file in c?
It's been a long time since I've done something like this in C.
There is only one good way to my knowledge. If the length of the dimensions change, then you first need to write out the number of dimensions. Then perform a loop within a loop within a loop to write out each element. For example:
int xLength = 10;
int yLength = 10;
int zLength = 10;
char myBigArray[xLength][yLength][zLength];
// Do something to fill the array.
f1 = fopen("myfile.dat", "wt")
for(int x = 0;x %26lt; 10;x++)
for(int y = 0;y %26lt; 10;y++)
for(int z = 0;z %26lt; 10;z++)
{
fprintf(f1, "%d\n", myBigArray[x][y][z]);
}
However, I think that it would be better if you used XML, but I'm not sure how to do that effeciently in C.
Reply:arrays are stored contiguously.SO you need not use 3 for loops.Jus open the file and fwrite((void*)name_of_3d_array,sizeof(ea...
How to write a 3dimensional array to a file in c?
It's been a long time since I've done something like this in C.
There is only one good way to my knowledge. If the length of the dimensions change, then you first need to write out the number of dimensions. Then perform a loop within a loop within a loop to write out each element. For example:
int xLength = 10;
int yLength = 10;
int zLength = 10;
char myBigArray[xLength][yLength][zLength];
// Do something to fill the array.
f1 = fopen("myfile.dat", "wt")
for(int x = 0;x %26lt; 10;x++)
for(int y = 0;y %26lt; 10;y++)
for(int z = 0;z %26lt; 10;z++)
{
fprintf(f1, "%d\n", myBigArray[x][y][z]);
}
However, I think that it would be better if you used XML, but I'm not sure how to do that effeciently in C.
Reply:arrays are stored contiguously.SO you need not use 3 for loops.Jus open the file and fwrite((void*)name_of_3d_array,sizeof(ea...
C++ finding the mode of an array?
How would you go about finding the mode of an array? Can you show me some code as to how you can do that? My teacher says you can use an array to store the counts, but I dunno how. Where would the counters be?
C++ finding the mode of an array?
Well, you could designate the first n entry in the array to store information about the array if you wish. Of course that imply that you remember this each time you do any work on the array and that the type of the element is fit to hold the kind of information you wanted to store.
Let's suppose you have an array of int.
int myIntArray[60];
// now designate the first position to hold the array size
myIntArray[0] = 60; // but 59 values since this one is wasted
Now to answer your question about the mode, I assume here we are talking about stats (the mode being the most often occurring value). You basically need to count each time a specific value occur. There is many way to do it, but probably the most compact would be to use a std::map. For each value present in the array, create an entry in the map, with the value as the key and it's occurrence as the value, which you increment each time you find it in the array.
If you are not familiar with std::map, you can use an other array to hold the occurrence of each of the values. The problem with this is that you don't know in advance the range of values in your first array so it can go from minus infinity to infinity. It could require quite a large one. email me if you need a more precise description of that.
And then to keep in line with the rest of the question, you could store the mode once computed into the array the same way we did for it's size. That way you don't need to recompute it the next time you need it. Unless of course you change the content of the array.
myIntArray[1] = ComputeTheMode(myIntArray);
// now only 58 remaining spot for values
Obviously though, the preferred way to go when you need to store additional information like that, would be to create a struct or a class, have the array, it's size, and any additional information stored as member of it. Not as part of the array itself. Better yet, never use arrays. std::vector and other stl containers are so much better. This is c++ after all. Use it's features anywhere you can.
Hope that help
Reply:Look at http://www.daniweb.com/techtalkforums/th...
Anyway, one way in C++ is:
1. Your numbers are in a vector (http://www.cppreference.com/cppvector/in... ). I'm guessing vector is a better choice because you probably have no idea how many numbers you are doing to be dealing with, right?
2. Use a map to store the frequency of each number. Walk through the vector of numbers, incrementing the frequency of the corresponding number in the map.
3. Then go through the map itself. Look for what frequency is the largest. That will tell you your mode.
Reply:you have to keep track of the size of the array seperately. create a new variable for that.
C++ finding the mode of an array?
Well, you could designate the first n entry in the array to store information about the array if you wish. Of course that imply that you remember this each time you do any work on the array and that the type of the element is fit to hold the kind of information you wanted to store.
Let's suppose you have an array of int.
int myIntArray[60];
// now designate the first position to hold the array size
myIntArray[0] = 60; // but 59 values since this one is wasted
Now to answer your question about the mode, I assume here we are talking about stats (the mode being the most often occurring value). You basically need to count each time a specific value occur. There is many way to do it, but probably the most compact would be to use a std::map. For each value present in the array, create an entry in the map, with the value as the key and it's occurrence as the value, which you increment each time you find it in the array.
If you are not familiar with std::map, you can use an other array to hold the occurrence of each of the values. The problem with this is that you don't know in advance the range of values in your first array so it can go from minus infinity to infinity. It could require quite a large one. email me if you need a more precise description of that.
And then to keep in line with the rest of the question, you could store the mode once computed into the array the same way we did for it's size. That way you don't need to recompute it the next time you need it. Unless of course you change the content of the array.
myIntArray[1] = ComputeTheMode(myIntArray);
// now only 58 remaining spot for values
Obviously though, the preferred way to go when you need to store additional information like that, would be to create a struct or a class, have the array, it's size, and any additional information stored as member of it. Not as part of the array itself. Better yet, never use arrays. std::vector and other stl containers are so much better. This is c++ after all. Use it's features anywhere you can.
Hope that help
Reply:Look at http://www.daniweb.com/techtalkforums/th...
Anyway, one way in C++ is:
1. Your numbers are in a vector (http://www.cppreference.com/cppvector/in... ). I'm guessing vector is a better choice because you probably have no idea how many numbers you are doing to be dealing with, right?
2. Use a map to store the frequency of each number. Walk through the vector of numbers, incrementing the frequency of the corresponding number in the map.
3. Then go through the map itself. Look for what frequency is the largest. That will tell you your mode.
Reply:you have to keep track of the size of the array seperately. create a new variable for that.
How to fill an array in a function with c++?
how to pass the parameters?
is there any other way without pointer and please help me because I need a function that fill an array
How to fill an array in a function with c++?
Pass it normally, using the correct declaration. That is, if you had char someVar[], pass it to the function as char functionVar[]. Then treat the function argument as you would the real array.
%26gt; is there any other way without pointer
Read http://c-faq.com/aryptr/index.html . There's an equivalence between pointers and arrays in some cases, so that's why you can work with the formal argument like you would with a pointer. Go through the C FAQ I linked to. It's confusing, and takes time to digest.
If you really want to understand C++, you should get a serious book. For C++, it's C++ Primer by Lippman or Accelerated C++ by Koenig (both are suitable for beginners). To understand the C portion of C++, you may want to get yourself K%26amp;R's The C Programming Language.
Reply:you can't modify an array if you pass by value into a function you have to pass by referance. When you pass by value a copy of the array(or any object) is put on the stack( or heap) . The keyword here is copy. I'm sry but you have to pass by reference otherwise you break the rules of encapsulation.
is there any other way without pointer and please help me because I need a function that fill an array
How to fill an array in a function with c++?
Pass it normally, using the correct declaration. That is, if you had char someVar[], pass it to the function as char functionVar[]. Then treat the function argument as you would the real array.
%26gt; is there any other way without pointer
Read http://c-faq.com/aryptr/index.html . There's an equivalence between pointers and arrays in some cases, so that's why you can work with the formal argument like you would with a pointer. Go through the C FAQ I linked to. It's confusing, and takes time to digest.
If you really want to understand C++, you should get a serious book. For C++, it's C++ Primer by Lippman or Accelerated C++ by Koenig (both are suitable for beginners). To understand the C portion of C++, you may want to get yourself K%26amp;R's The C Programming Language.
Reply:you can't modify an array if you pass by value into a function you have to pass by referance. When you pass by value a copy of the array(or any object) is put on the stack( or heap) . The keyword here is copy. I'm sry but you have to pass by reference otherwise you break the rules of encapsulation.
In C# how do I search an array to see if it already contains a particular number?
The user enters a number. I have have to search through the array to see if it already contains this number. If it does, then it will be disregarded so that their are only unique numbers in the array. If the number is not already in the array, it will add it to the array.
I have the array already denifined by the number that the user inputs, I just do not know how to search the array to see if that number is there or not. I was using if statements to compare the number to each element of the array, but that would get rid of both copies of the number instead of keeping one. Please help!
In C# how do I search an array to see if it already contains a particular number?
With Array.IndexOf you can easily check the existence of an element without using predicates. Use the following code as a guideline:
type[] myArray = whatever;
if (Array.IndexOf(myArray, theNumber) != -1)
{
// The number already exists in the array
}
else
{
// The number does not exist in the array
}
Reply:I don't know C#, but here's pseudocode:
boolean isInArray(int array[], int number)
{
for (x=0; x%26lt;=array.length;x++)
{
if (array[x] == number)
return true;
}
return false;
}
Then call that method in another function to put the number in or not depending on if it's true or not. You would also want to use a Vector (i'm not sure about C#'s array abilities but arrays are typically static meaning you can't add more elements after it's declared) so you can dynamically add more elements for as long as the person inputs numbers. Vectors work roughly the same way as arrays, but the number of elements can be dynamically changed. That and the vectors will have their own methods (at least they do in Java which C# imitates).
floral centerpieces
I have the array already denifined by the number that the user inputs, I just do not know how to search the array to see if that number is there or not. I was using if statements to compare the number to each element of the array, but that would get rid of both copies of the number instead of keeping one. Please help!
In C# how do I search an array to see if it already contains a particular number?
With Array.IndexOf you can easily check the existence of an element without using predicates. Use the following code as a guideline:
type[] myArray = whatever;
if (Array.IndexOf(myArray, theNumber) != -1)
{
// The number already exists in the array
}
else
{
// The number does not exist in the array
}
Reply:I don't know C#, but here's pseudocode:
boolean isInArray(int array[], int number)
{
for (x=0; x%26lt;=array.length;x++)
{
if (array[x] == number)
return true;
}
return false;
}
Then call that method in another function to put the number in or not depending on if it's true or not. You would also want to use a Vector (i'm not sure about C#'s array abilities but arrays are typically static meaning you can't add more elements after it's declared) so you can dynamically add more elements for as long as the person inputs numbers. Vectors work roughly the same way as arrays, but the number of elements can be dynamically changed. That and the vectors will have their own methods (at least they do in Java which C# imitates).
floral centerpieces
C++ finding limits of a sorted array?
How would you go about finding the highest and lowest scores in an array that is already sorted? I know how to find the highest and lowest scores when it is not sorted, but if it is sorted there must be a simpler way to find the limits besides searching the entire array. Here is the problem:
"findlimits() which will find the highest SAT score and the lowest SAT score and return both to the main program using reference parameters. Note that this is extremely easy once the score array has been sorted. The findlimits() function receives four parameters, the array scores, the number of values in the array n, and reference parameters max and min. After returning to main, main should print the high and low scores together with messages."
Why is it easy once the array is sorted, that means it must be a simple way. And how would I do that???
C++ finding limits of a sorted array?
I might be overlooking something, but if you know the array is of length n, and it is sorted in ascending order, then
int min = array[0];
int max = array[n-1];
Reply:There are 2 ways to go about it. One way is to use the min %26amp; max variables to store the highest %26amp; lowest values during the sorting routine.
(This is the best way.)
Another way is to store the 1st byte and the last byte from the array from outside or within the sorting routine. You need to know the length of the array though.
"findlimits() which will find the highest SAT score and the lowest SAT score and return both to the main program using reference parameters. Note that this is extremely easy once the score array has been sorted. The findlimits() function receives four parameters, the array scores, the number of values in the array n, and reference parameters max and min. After returning to main, main should print the high and low scores together with messages."
Why is it easy once the array is sorted, that means it must be a simple way. And how would I do that???
C++ finding limits of a sorted array?
I might be overlooking something, but if you know the array is of length n, and it is sorted in ascending order, then
int min = array[0];
int max = array[n-1];
Reply:There are 2 ways to go about it. One way is to use the min %26amp; max variables to store the highest %26amp; lowest values during the sorting routine.
(This is the best way.)
Another way is to store the 1st byte and the last byte from the array from outside or within the sorting routine. You need to know the length of the array though.
Write a program that will let the user type in 10 integer values and store them in an array using the C++?
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
int myArray(10); // declare your array and size
/*
Create a loop to continuously ask for the user's input until
integer #10 is achieved
*/
for(int count = 0; count %26lt; myArray; count++)
{
// Prompt for input
cout %26lt;%26lt; "Type integer #" %26lt;%26lt; count + 1 %26lt;%26lt; ": ";
// Input to the specific array # specified by the variable "count"
cin %26lt;%26lt; myArray(count);
}
}
Write a program that will let the user type in 10 integer values and store them in an array using the C++?
#include%26lt;iostream.h%26gt;
#include%26lt;conio.h%26gt;
void main()
{
clrscr();
int num[10];
cout%26lt;%26lt;"Enter 10 numbers:";
for(int i=0;i%26lt;10;i++)
{
cin%26gt;%26gt;num[i];
}
cout%26lt;%26lt;"The stored elements of the array are:";
for(i=0;i%26lt;10;i++)
cout%26lt;%26lt;num[i];
getch();
}
Reply:no, i will not write the program, you should do it your self. but i'll write a harsh function in c of course:
int[10] array;
for(int i=0;i%26lt;10;i++)
{
cout%26gt;%26gt;type some words
cin%26lt;%26lt; %26amp;array[i];
}
// it'll do the basic function you ask.
// you should repair any function that doesn't work, because its been a while from the last time i wrote program in c++.
using namespace std;
int main()
{
int myArray(10); // declare your array and size
/*
Create a loop to continuously ask for the user's input until
integer #10 is achieved
*/
for(int count = 0; count %26lt; myArray; count++)
{
// Prompt for input
cout %26lt;%26lt; "Type integer #" %26lt;%26lt; count + 1 %26lt;%26lt; ": ";
// Input to the specific array # specified by the variable "count"
cin %26lt;%26lt; myArray(count);
}
}
Write a program that will let the user type in 10 integer values and store them in an array using the C++?
#include%26lt;iostream.h%26gt;
#include%26lt;conio.h%26gt;
void main()
{
clrscr();
int num[10];
cout%26lt;%26lt;"Enter 10 numbers:";
for(int i=0;i%26lt;10;i++)
{
cin%26gt;%26gt;num[i];
}
cout%26lt;%26lt;"The stored elements of the array are:";
for(i=0;i%26lt;10;i++)
cout%26lt;%26lt;num[i];
getch();
}
Reply:no, i will not write the program, you should do it your self. but i'll write a harsh function in c of course:
int[10] array;
for(int i=0;i%26lt;10;i++)
{
cout%26gt;%26gt;type some words
cin%26lt;%26lt; %26amp;array[i];
}
// it'll do the basic function you ask.
// you should repair any function that doesn't work, because its been a while from the last time i wrote program in c++.
I want a c program using pointer,array,functions of postal deposit can anyone help as soon as possible?
is what you want a postal deposit program?
tell me the specifications of it and the allowed designs (do you allow structs, classes, etc.?)
tell me how the postal deposit is supposed to work as well.
tell me the specifications of it and the allowed designs (do you allow structs, classes, etc.?)
tell me how the postal deposit is supposed to work as well.
Does anybody know how to use C++ arrays?
Please help me with this array declaration problem.
Write array declarations for the following:
a. a list of 100 double-precision voltages
b. a list of 50 double-precision temperatures
c. a list of 30 characters, each representing a code
d. a list of 100 integer years
e. a list of 32 double precision velocities
f. a list of 1000 double-precision distances
g. a list of 6 integer code numbers
Does anybody know how to use C++ arrays?
Frank,
I take it from you questions that you are just starting out in C++.
I also take it from you questions that you are not prepared to answer them.
So my next question is what the hell are you doing on a C++ course?
If you are not prepared to take the time and trouble to attempt these simple, yes simple, questions then I would seriously think about dropping the course now.
Reply:Well the first thing you need to decide is if you want to use native C++ arrays (built-in to the language, fixed size, declared like "int x[2]") or STL vectors (more powerful, standard library class, re-sizable). For now we'll assume you want standard c++ arrays.
(a) list of 100 double voltages
//standard declaration
double voltages[100];
voltage[0] = 4.5;
voltage[1] = 3.2;
....
//OR initializer list
double voltages[] = {4.5, 3.2, ..........};
(b) same as (a) except only listing 50 values in the declaration or initializer list
(c)
char codes[30];
code[0] = 'a';
....
//OR
char codes[] = { 'g', 'c', 't', 'a', 'c' };
(d)
int years[100];
//you get the idea
You can figure out the rest
wedding florist
Write array declarations for the following:
a. a list of 100 double-precision voltages
b. a list of 50 double-precision temperatures
c. a list of 30 characters, each representing a code
d. a list of 100 integer years
e. a list of 32 double precision velocities
f. a list of 1000 double-precision distances
g. a list of 6 integer code numbers
Does anybody know how to use C++ arrays?
Frank,
I take it from you questions that you are just starting out in C++.
I also take it from you questions that you are not prepared to answer them.
So my next question is what the hell are you doing on a C++ course?
If you are not prepared to take the time and trouble to attempt these simple, yes simple, questions then I would seriously think about dropping the course now.
Reply:Well the first thing you need to decide is if you want to use native C++ arrays (built-in to the language, fixed size, declared like "int x[2]") or STL vectors (more powerful, standard library class, re-sizable). For now we'll assume you want standard c++ arrays.
(a) list of 100 double voltages
//standard declaration
double voltages[100];
voltage[0] = 4.5;
voltage[1] = 3.2;
....
//OR initializer list
double voltages[] = {4.5, 3.2, ..........};
(b) same as (a) except only listing 50 values in the declaration or initializer list
(c)
char codes[30];
code[0] = 'a';
....
//OR
char codes[] = { 'g', 'c', 't', 'a', 'c' };
(d)
int years[100];
//you get the idea
You can figure out the rest
wedding florist
Function in c++ to reverse an array of float elements?
http://cppreference.com/cppalgorithm/rev...
Store all the floats in a vector, and then use reverse().
vector%26lt;float%26gt; numbers;
numbers.push_back(3.1f);
numbers.push_back(1.f);
numbers.push_back(7.3916f);
reverse(numbers.begin(), numbers.end());
Function in c++ to reverse an array of float elements?
usin temp variable u can reverse the numbers.
float arr[10];//array of elements
int temp;
for(int i=0;i%26lt;max;i++) //max-%26gt;highest no. of elements.
{for(int j=max-1;j%26gt;=0;j--)
{temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
thats it the array is swapped..
Hope this helps u
Reply:You probably need to create a second array and then get the last elements from the first array and put then in the front of the second array.
Something like this...
# Elements in a
Create 2nd array same size
then something using a while like...
b[firstelement]=a[lastelement]
then
b[1]=a[lastelement-1]
etc.
Just create your own.
Store all the floats in a vector, and then use reverse().
vector%26lt;float%26gt; numbers;
numbers.push_back(3.1f);
numbers.push_back(1.f);
numbers.push_back(7.3916f);
reverse(numbers.begin(), numbers.end());
Function in c++ to reverse an array of float elements?
usin temp variable u can reverse the numbers.
float arr[10];//array of elements
int temp;
for(int i=0;i%26lt;max;i++) //max-%26gt;highest no. of elements.
{for(int j=max-1;j%26gt;=0;j--)
{temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
thats it the array is swapped..
Hope this helps u
Reply:You probably need to create a second array and then get the last elements from the first array and put then in the front of the second array.
Something like this...
# Elements in a
Create 2nd array same size
then something using a while like...
b[firstelement]=a[lastelement]
then
b[1]=a[lastelement-1]
etc.
Just create your own.
In c++,why is an array called a derived data type?
Arrays refer to a list of finite number of same data types. The data in the array can be accessed by an index number ranging from 0 to n(where n is the number of data element it can store). Ex- if arr[3] is an array of int(egers) then the different values in the array can be accessed as shown below. arr[0], arr[1],arr[2] when we declare an array such as the one sown above then by arr[3] we mean that we want three elements in the array and hence while accessing arr[2] is the last element.
In c++,why is an array called a derived data type?
All details about c++ language is in the website www.cplusplus.com.so pls refer there.
Reply:the array could be functionally called the data.
Reply:First you gotta know what a primitive data type is . Primitives are like integers, floats, doubles. But an array actually consists of a bunch of one type of these primitives, thus being a derived data type . I hope this helps...
In c++,why is an array called a derived data type?
All details about c++ language is in the website www.cplusplus.com.so pls refer there.
Reply:the array could be functionally called the data.
Reply:First you gotta know what a primitive data type is . Primitives are like integers, floats, doubles. But an array actually consists of a bunch of one type of these primitives, thus being a derived data type . I hope this helps...
Write a program to find the sum of the array elements in c language?
Why don't you show us what you have so far. This is really quite a simple use of a loop and some basic math. Show us what you have so far, and maybe we can give you some specific help instead of just writing it for you.
Write a program to find the sum of the array elements in c language?
You can do it with loop.
1. Declare variable outside of loop.
2. Loop through the array and look at each elements in array and add them in each iteration.
Write a program to find the sum of the array elements in c language?
You can do it with loop.
1. Declare variable outside of loop.
2. Loop through the array and look at each elements in array and add them in each iteration.
C++ Arrays?
i have to make a c++ program using arrays, that adds 2 integers up to 20 characters long. right now, i'm stuck before i even started because my array only outputs correctly if the input number is 10 characters long. what's wrong with this?
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
using namespace std;
int main()
{
int augent[20];
int index;
for(index = 0; index %26lt;20; index++)
{
augent [index]=0.0;
cout%26lt;%26lt;"Please enter augent: ";
cin%26gt;%26gt;augent[index];
cout%26lt;%26lt;endl;
cout%26lt;%26lt;"Augent is "%26lt;%26lt;augent[index]%26lt;%26lt;endl;
}
return 0;
}
Also, once this problem is solved how do i proceed with the rest of the program?
Also I ve been told I have to use strings, is that true?
I would greatly appreciate your help.
C++ Arrays?
Type | Bytes | range of values
--------------------------------------...
int 4 –2,147,483,648 to 2,147,483,647
unsigned int 4 0 to 4,294,967,295
--------------------------------------...
main problem is that you CANT have a 20 digit INT, what u need is either a __int64 (long long) or an unsigned __int64 (unsigned long long) these allow –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 and 0 to 18,446,744,073,709,551,615 respectivly as they are 8 byte,
that or choose another way to store them (maybe as a text value, as there is a c++ function to convert btw text and numbers (or just write your own))
Reply:integers have a range which they work in I think 20 characters
eg 100000000000000000000 is out of the range of an integer. Try floats I am not sure how much they hold or use strings then convert them into ints.
Also why does your array have 20 elements isnt it suposed to be 2 elements 20 char long each? maybe i dont get the question.
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
using namespace std;
int main()
{
int augent[20];
int index;
for(index = 0; index %26lt;20; index++)
{
augent [index]=0.0;
cout%26lt;%26lt;"Please enter augent: ";
cin%26gt;%26gt;augent[index];
cout%26lt;%26lt;endl;
cout%26lt;%26lt;"Augent is "%26lt;%26lt;augent[index]%26lt;%26lt;endl;
}
return 0;
}
Also, once this problem is solved how do i proceed with the rest of the program?
Also I ve been told I have to use strings, is that true?
I would greatly appreciate your help.
C++ Arrays?
Type | Bytes | range of values
--------------------------------------...
int 4 –2,147,483,648 to 2,147,483,647
unsigned int 4 0 to 4,294,967,295
--------------------------------------...
main problem is that you CANT have a 20 digit INT, what u need is either a __int64 (long long) or an unsigned __int64 (unsigned long long) these allow –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 and 0 to 18,446,744,073,709,551,615 respectivly as they are 8 byte,
that or choose another way to store them (maybe as a text value, as there is a c++ function to convert btw text and numbers (or just write your own))
Reply:integers have a range which they work in I think 20 characters
eg 100000000000000000000 is out of the range of an integer. Try floats I am not sure how much they hold or use strings then convert them into ints.
Also why does your array have 20 elements isnt it suposed to be 2 elements 20 char long each? maybe i dont get the question.
How to find the product of 2d array matrices in c?
Is this a homework assignment?
MultMatrix(firstM, secondM, outM, firstrows, cols, secondcols)
Matrix firstM, secondM, outM;
int firstrows, cols, secondcols;
{
int i,j,k;
double sum;
for(i=0; i %26lt; secondcols; i++)
for(j=0; j %26lt; firstrows; j++)
{
sum = 0.0;
for(k=0; k %26lt; cols; k++)
sum += firstM[j][k] * secondM[k][i];
outM[j][i] = sum;
}
}
MultMatrix(firstM, secondM, outM, firstrows, cols, secondcols)
Matrix firstM, secondM, outM;
int firstrows, cols, secondcols;
{
int i,j,k;
double sum;
for(i=0; i %26lt; secondcols; i++)
for(j=0; j %26lt; firstrows; j++)
{
sum = 0.0;
for(k=0; k %26lt; cols; k++)
sum += firstM[j][k] * secondM[k][i];
outM[j][i] = sum;
}
}
C++ reading from file into array using a function?
text file contains title of store then a product code, product name, then product price. so type string, string, long respectively. program compiles and runs fine until count is returned in main. something is wrong with the loop and i don't know what. this is the important stuff:
--------------------------- function: ----------------------------
long readFile (string code[], string product[], long price[])
{
const long ITEMS = 24;
string temp,
store;
long count;
ifstream fin ("program5.txt");
getline(fin,store);
count=0;
fin%26gt;%26gt;temp;
while (!fin.eof())
{
temp=code[count];
getline(fin,product[count]);
fin%26gt;%26gt;price[count];
count++;
fin%26gt;%26gt;temp;
}
fin.close();
return count;
}
----------------from main:-----------------
.......
count=readFile(code,product,price);
fout%26lt;%26lt;count%26lt;%26lt;endl;
for (pos=0; pos%26lt;count; pos++)
{
fout%26lt;%26lt;code[pos]%26lt;%26lt;endl;
fout%26lt;%26lt;product[pos]%26lt;%26lt;endl;
fout%26lt;%26lt;price[pos]%26lt;%26lt;endl;
}
....
if more info is needed i can provide it. reminder this is c++
C++ reading from file into array using a function?
count looks ok--not sure why count is giving you a problem.
Look at the line just following the while declaration. You have the left side and right side inverted. Use:
code[count] = temp;
--------------------------- function: ----------------------------
long readFile (string code[], string product[], long price[])
{
const long ITEMS = 24;
string temp,
store;
long count;
ifstream fin ("program5.txt");
getline(fin,store);
count=0;
fin%26gt;%26gt;temp;
while (!fin.eof())
{
temp=code[count];
getline(fin,product[count]);
fin%26gt;%26gt;price[count];
count++;
fin%26gt;%26gt;temp;
}
fin.close();
return count;
}
----------------from main:-----------------
.......
count=readFile(code,product,price);
fout%26lt;%26lt;count%26lt;%26lt;endl;
for (pos=0; pos%26lt;count; pos++)
{
fout%26lt;%26lt;code[pos]%26lt;%26lt;endl;
fout%26lt;%26lt;product[pos]%26lt;%26lt;endl;
fout%26lt;%26lt;price[pos]%26lt;%26lt;endl;
}
....
if more info is needed i can provide it. reminder this is c++
C++ reading from file into array using a function?
count looks ok--not sure why count is giving you a problem.
Look at the line just following the while declaration. You have the left side and right side inverted. Use:
code[count] = temp;
Thursday, July 30, 2009
How to sort a One-Dimensional Array in Turbo C?
5 Numbers or more
How to sort a One-Dimensional Array in Turbo C?
Bubble Sort
The bubble sort gets its name because as elements are sorted they gradually "bubble" (or rise) to their proper positions, like bubbles rising in a glass of soda. The bubble sort repeatedly compares adjacent elements of an array, starting with the first and second elements, and swapping them if they are out of order. After the first and second elements are compared, the second and third elements are compared, and swapped if they are out of order. This process continues until the end of the list is reached.
When the end is reached, the bubble sort returns to elements one and two and starts the process all over again. So, when does it stop? The bubble sort knows that it is finished when it examines the entire array and no "swaps" are needed (thus the list is in proper order). The bubble sort keeps track of the occurrence of swaps by the use of a flag.
The table below follows an array of numbers before, during, and after a bubble sort for descending order. A "pass" is defined as one full trip through the array comparing and if necessary, swapping, adjacent elements. Several passes have to be made through the array before it is finally sorted.
Array at beginning: 84 69 76 86 94 91
After Pass #1: 84 76 86 94 91 69
After Pass #2: 84 86 94 91 76 69
After Pass #3: 86 94 91 84 76 69
After Pass #4: 94 91 86 84 76 69
After Pass #5 (done): 94 91 86 84 76 69
The bubble sort is an easy algorithm to program, but it is slower than many other sorts. With a bubble sort, it is always necessary to make one final "pass" through the array to check to see that no swaps are made to ensure that the process is finished. In actuality, the process is finished before this last pass is made.
// Bubble Sort Function for Descending Order
void main()
{
int i, j, flag = 1; // set flag to 1 to begin initial pass
int temp; // holding variable
int arrayLength = array.length( );
for(i = 1; (i %26lt;= arrayLength) %26amp;%26amp; flag; i++)
{
flag = 0;
for (j=0; j %26lt; (arrayLength -1); j++)
{
if (array[j+1] %26gt; array[j]) // ascending order simply changes to %26lt;
{
temp = array[j]; // swap elements
array[j] = array[j+1];
array[j+1] = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
}
Reply:Well, assuming a size parameter (and the array being passed simulated call by reference with ints):
//Begin function BubbleSort
void BubbleSort(int arr[], int size)
{
int i, j, temp;
for(j=0; j%26lt;size; j++)
for(i=0; i%26lt;size-1; i++)
if(arr[i]%26gt;arr[i+1]){
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;}
}
//End function BubbleSort
This isn't the fastest sort, but it's easy and common.
The quickest way to find the size of an array:
sizeof(arr)/sizeof(*arr);
Note that *arr dereferences the constant pointer to the first element of the array.
Reply:*barfs*
Two people suggesting the most inefficient sorting algorithm in existence. Read this article to learn how to really sort data.
http://eternallyconfuzzled.com/tuts/algo...
In reality, for the size of the array that you will be sorting, the time a bubble sort will take vs a more efficient sort is negligible and you should still know about it. Regardless, you should also know you other (better) options. I won't get down too much on the answers above me because they both did acknowledge that it is an inefficient sort, however... still read his article, it is good.
How to sort a One-Dimensional Array in Turbo C?
Bubble Sort
The bubble sort gets its name because as elements are sorted they gradually "bubble" (or rise) to their proper positions, like bubbles rising in a glass of soda. The bubble sort repeatedly compares adjacent elements of an array, starting with the first and second elements, and swapping them if they are out of order. After the first and second elements are compared, the second and third elements are compared, and swapped if they are out of order. This process continues until the end of the list is reached.
When the end is reached, the bubble sort returns to elements one and two and starts the process all over again. So, when does it stop? The bubble sort knows that it is finished when it examines the entire array and no "swaps" are needed (thus the list is in proper order). The bubble sort keeps track of the occurrence of swaps by the use of a flag.
The table below follows an array of numbers before, during, and after a bubble sort for descending order. A "pass" is defined as one full trip through the array comparing and if necessary, swapping, adjacent elements. Several passes have to be made through the array before it is finally sorted.
Array at beginning: 84 69 76 86 94 91
After Pass #1: 84 76 86 94 91 69
After Pass #2: 84 86 94 91 76 69
After Pass #3: 86 94 91 84 76 69
After Pass #4: 94 91 86 84 76 69
After Pass #5 (done): 94 91 86 84 76 69
The bubble sort is an easy algorithm to program, but it is slower than many other sorts. With a bubble sort, it is always necessary to make one final "pass" through the array to check to see that no swaps are made to ensure that the process is finished. In actuality, the process is finished before this last pass is made.
// Bubble Sort Function for Descending Order
void main()
{
int i, j, flag = 1; // set flag to 1 to begin initial pass
int temp; // holding variable
int arrayLength = array.length( );
for(i = 1; (i %26lt;= arrayLength) %26amp;%26amp; flag; i++)
{
flag = 0;
for (j=0; j %26lt; (arrayLength -1); j++)
{
if (array[j+1] %26gt; array[j]) // ascending order simply changes to %26lt;
{
temp = array[j]; // swap elements
array[j] = array[j+1];
array[j+1] = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
}
Reply:Well, assuming a size parameter (and the array being passed simulated call by reference with ints):
//Begin function BubbleSort
void BubbleSort(int arr[], int size)
{
int i, j, temp;
for(j=0; j%26lt;size; j++)
for(i=0; i%26lt;size-1; i++)
if(arr[i]%26gt;arr[i+1]){
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;}
}
//End function BubbleSort
This isn't the fastest sort, but it's easy and common.
The quickest way to find the size of an array:
sizeof(arr)/sizeof(*arr);
Note that *arr dereferences the constant pointer to the first element of the array.
Reply:*barfs*
Two people suggesting the most inefficient sorting algorithm in existence. Read this article to learn how to really sort data.
http://eternallyconfuzzled.com/tuts/algo...
In reality, for the size of the array that you will be sorting, the time a bubble sort will take vs a more efficient sort is negligible and you should still know about it. Regardless, you should also know you other (better) options. I won't get down too much on the answers above me because they both did acknowledge that it is an inefficient sort, however... still read his article, it is good.
C++ coding for rotating 2D array to 90 degrees clockwise.?
i'm having problem with writing a program for rotating a 2D array by 90 degrees clockwise.
Example:
1 2 3
4 5 6
7 8 9
to
7 4 1
8 5 2
9 6 3
i'm doing the rotation in function. please write the coding for rotation. TQ.
C++ coding for rotating 2D array to 90 degrees clockwise.?
No Kartoo is wrong, Only transposing would not rotate. To Rotate, you have to reflect the matrix . Refflection about horizontal axis will ratate the matrix clockwise while reflection about vertical axis will make it rotate counter clockwise,
I am giving you a pseodo code of rotation of m by n matrix.
for(i=0; i%26lt;m;i++)
for(j=0; j%26lt;n; j++)
newArray[i][j]=oldArray[j][m-i+1];
This will cause the matrix to rotate counter-clockwise, itnerchange of i and j transposes the matrix while sustituting i by m-i+1 causes a reflection respect to vertical axis.
Reply:public static int[][] transpose(int[][] a, int m, int n)
{
// create new matrix
int[][] b = new int[n][m];
for (int i = 0; i %26lt; n; i++)
for (int j = 0; j %26lt; m; j++)
b[i][j] = a[j][i];
return b;
}
main()
{
int a[3][3];
for(int i=0;i%26lt;3;i++)
{
for(int j=0;j%26lt;3;j++)
{
cin%26gt;%26gt;a[i][j];
}
}
a = transpose(a, 3, 3);
}
Reply:in your example you have give 3D matrix
2D array looks like
1 2
3 4
(0r)
1 6
2 7
3 8
4 9
5 10
so i think the above code needs to be reconstructed.
When the no. of rows and columns are same it is easy. What if they are not equal?
Let us in this dimension...
Example:
1 2 3
4 5 6
7 8 9
to
7 4 1
8 5 2
9 6 3
i'm doing the rotation in function. please write the coding for rotation. TQ.
C++ coding for rotating 2D array to 90 degrees clockwise.?
No Kartoo is wrong, Only transposing would not rotate. To Rotate, you have to reflect the matrix . Refflection about horizontal axis will ratate the matrix clockwise while reflection about vertical axis will make it rotate counter clockwise,
I am giving you a pseodo code of rotation of m by n matrix.
for(i=0; i%26lt;m;i++)
for(j=0; j%26lt;n; j++)
newArray[i][j]=oldArray[j][m-i+1];
This will cause the matrix to rotate counter-clockwise, itnerchange of i and j transposes the matrix while sustituting i by m-i+1 causes a reflection respect to vertical axis.
Reply:public static int[][] transpose(int[][] a, int m, int n)
{
// create new matrix
int[][] b = new int[n][m];
for (int i = 0; i %26lt; n; i++)
for (int j = 0; j %26lt; m; j++)
b[i][j] = a[j][i];
return b;
}
main()
{
int a[3][3];
for(int i=0;i%26lt;3;i++)
{
for(int j=0;j%26lt;3;j++)
{
cin%26gt;%26gt;a[i][j];
}
}
a = transpose(a, 3, 3);
}
Reply:in your example you have give 3D matrix
2D array looks like
1 2
3 4
(0r)
1 6
2 7
3 8
4 9
5 10
so i think the above code needs to be reconstructed.
When the no. of rows and columns are same it is easy. What if they are not equal?
Let us in this dimension...
How do i manipulate an array using visual c++ such that i can find the max and min values?
Hell that last one was complex.
Just sort the array with qsort (see link) and then take the [0] and [max] values.
How do i manipulate an array using visual c++ such that i can find the max and min values?
RTFM?
Please read the link
Reply:#include %26lt;iostream%26gt;
#include %26lt;algorithm%26gt;// for C++ sort function
using namespace std;
const int SIZE = 10000;
void GetInputArray (int%26amp; userSize, int originalArray []);
void FindMinMax (int userSize, int%26amp; min, int%26amp; max, int originalArray []);
void Sort (int userSize, int originalArray [], int lowSortedArray [], int highSortedArray []);
void SplitArrayProcessing (int userSize, int originalArray [], int lowSplitArray [], int highSplitArray []);
int main ()
{
int originalArrayMain [SIZE];
int lowSortedArrayMain [SIZE];
int highSortedArrayMain [SIZE];
int lowSplitArrayMain [SIZE];
int highSplitArrayMain [SIZE];
int userSizeMain;
int minimum, maximum;
GetInputArray (userSizeMain, originalArrayMain);
if (userSizeMain %26gt; 0)
{
FindMinMax (userSizeMain, minimum, maximum, originalArrayMain);
Sort (userSizeMain, originalArrayMain, lowSortedArrayMain, highSortedArrayMain);
SplitArrayProcessing (userSizeMain, originalArrayMain, lowSplitArrayMain, highSplitArrayMain);
//outputs
cout %26lt;%26lt; "Min: " %26lt;%26lt; minimum %26lt;%26lt; endl;//minimum number in array
cout %26lt;%26lt; "Max: " %26lt;%26lt; maximum %26lt;%26lt; endl;//maximum number in array
cout %26lt;%26lt; "Low to High Sort: ";//low to high sorted array
for (int i = 0; i %26lt;= userSizeMain - 1; i++)
{
cout %26lt;%26lt; lowSortedArrayMain [i] %26lt;%26lt; " ";
}
cout %26lt;%26lt; endl %26lt;%26lt; "High to Low Sort: ";//high to low sorted array
for (int i = 0; i %26lt;= userSizeMain - 1; i++)
{
cout %26lt;%26lt; highSortedArrayMain [i] %26lt;%26lt; " ";
}
cout %26lt;%26lt; endl;
}
cout %26lt;%26lt; "First half split of array: ";//first half split of array
for (int i = 0; i %26lt;= ((userSizeMain/2) - 1); i++)
{
cout %26lt;%26lt; lowSplitArrayMain [i] %26lt;%26lt; " ";
}
cout %26lt;%26lt; endl;
cout %26lt;%26lt; "Second half split of array: ";// second half split of array
for (int i = 0; i %26lt; ((userSizeMain+1)/2); i++)
{
cout %26lt;%26lt; highSplitArrayMain [i] %26lt;%26lt; " ";
}
cout %26lt;%26lt; endl %26lt;%26lt; endl;
cout %26lt;%26lt; endl;
system("pause");
return 0;
};
void GetInputArray (int%26amp; userSize, int originalArray [])//Gets input array from user
{
cout %26lt;%26lt; "How many integers do you want to put into the array?: " %26lt;%26lt; endl;
cin %26gt;%26gt; userSize;
cout %26lt;%26lt; endl;
if (userSize %26gt; 0)
{
cout %26lt;%26lt; "Enter " %26lt;%26lt; userSize %26lt;%26lt; " integer(s) into the array (e.g. 5 88 4 etc):" %26lt;%26lt; endl;
for (int i = 0; i %26lt;= userSize - 1; i++)
{
cin %26gt;%26gt; originalArray [i];
}
cout %26lt;%26lt; endl;
}
else
{
cout %26lt;%26lt; "Invalid Entry!" %26lt;%26lt; endl;
}
}
void FindMinMax (int userSize, int%26amp; min, int%26amp; max, int originalArray [])
{
//find the mimimum number in the array
min = originalArray [0];
for (int i = 0; i %26lt;= userSize - 1; i++)
{
if (originalArray [i] %26lt;= min)
{
min = originalArray [i];
}
}
//find the maximum number in the array
max = originalArray [0];
for (int i = 0; i %26lt;= userSize - 1; i++)
{
if (originalArray [i] %26gt;= max)
{
max = originalArray [i];
}
}
}
void Sort (int userSize, int originalArray [], int lowSortedArray [], int highSortedArray [])
{
int j = (userSize - 1);
//copy contents of originalArray into lowSortedArray
for (int i = 0; i %26lt;= userSize - 1; i++)
{
lowSortedArray [i] = originalArray [i];
}
//sorts the lowSortedArray from low to high
sort (lowSortedArray, lowSortedArray + userSize);
//reverse copies the lowSortedArray to the highSortedArray (high to low sorted array)
for (int i = 0; i %26lt;= userSize - 1; i++)
{
if (j %26gt;= 0)
{
highSortedArray [j] = lowSortedArray [i];
j--;
}
}
}
void SplitArrayProcessing (int userSize, int originalArray [], int lowSplitArray [], int highSplitArray [])
{
int j = 0;
//splits the array in half
for (int i = 0; i %26lt;= userSize - 1; i++)
{
if (i %26lt; (userSize/2))
{
lowSplitArray [i] = originalArray [i];
}
else
{
highSplitArray [j] = originalArray [i];
j++;
}
}
}
Reply:// this is a simple example, but not necessarily the most efficient
int myArray[5] = {6,1,3,8,2}; // an array of 5 random integers
int min = myArray[0]; // use the first value in the array
int max = myArray[0]; // use the first value in the array
// the loop below compares min and max to each value in the array
for(int i = 1; i %26lt; 5; i++) // 5 is the size of the array
{
if(min %26gt; myArray[i]) min = myArray[i];
if(max %26lt; myArray[i]) max = myArray[i];
}
cout %26lt;%26lt; "MIN = " %26lt;%26lt; min %26lt;%26lt; endl; // prints the lowest number
cout %26lt;%26lt; "MAX = " %26lt;%26lt; max %26lt;%26lt; endl; // prints the highest number
Just sort the array with qsort (see link) and then take the [0] and [max] values.
How do i manipulate an array using visual c++ such that i can find the max and min values?
RTFM?
Please read the link
Reply:#include %26lt;iostream%26gt;
#include %26lt;algorithm%26gt;// for C++ sort function
using namespace std;
const int SIZE = 10000;
void GetInputArray (int%26amp; userSize, int originalArray []);
void FindMinMax (int userSize, int%26amp; min, int%26amp; max, int originalArray []);
void Sort (int userSize, int originalArray [], int lowSortedArray [], int highSortedArray []);
void SplitArrayProcessing (int userSize, int originalArray [], int lowSplitArray [], int highSplitArray []);
int main ()
{
int originalArrayMain [SIZE];
int lowSortedArrayMain [SIZE];
int highSortedArrayMain [SIZE];
int lowSplitArrayMain [SIZE];
int highSplitArrayMain [SIZE];
int userSizeMain;
int minimum, maximum;
GetInputArray (userSizeMain, originalArrayMain);
if (userSizeMain %26gt; 0)
{
FindMinMax (userSizeMain, minimum, maximum, originalArrayMain);
Sort (userSizeMain, originalArrayMain, lowSortedArrayMain, highSortedArrayMain);
SplitArrayProcessing (userSizeMain, originalArrayMain, lowSplitArrayMain, highSplitArrayMain);
//outputs
cout %26lt;%26lt; "Min: " %26lt;%26lt; minimum %26lt;%26lt; endl;//minimum number in array
cout %26lt;%26lt; "Max: " %26lt;%26lt; maximum %26lt;%26lt; endl;//maximum number in array
cout %26lt;%26lt; "Low to High Sort: ";//low to high sorted array
for (int i = 0; i %26lt;= userSizeMain - 1; i++)
{
cout %26lt;%26lt; lowSortedArrayMain [i] %26lt;%26lt; " ";
}
cout %26lt;%26lt; endl %26lt;%26lt; "High to Low Sort: ";//high to low sorted array
for (int i = 0; i %26lt;= userSizeMain - 1; i++)
{
cout %26lt;%26lt; highSortedArrayMain [i] %26lt;%26lt; " ";
}
cout %26lt;%26lt; endl;
}
cout %26lt;%26lt; "First half split of array: ";//first half split of array
for (int i = 0; i %26lt;= ((userSizeMain/2) - 1); i++)
{
cout %26lt;%26lt; lowSplitArrayMain [i] %26lt;%26lt; " ";
}
cout %26lt;%26lt; endl;
cout %26lt;%26lt; "Second half split of array: ";// second half split of array
for (int i = 0; i %26lt; ((userSizeMain+1)/2); i++)
{
cout %26lt;%26lt; highSplitArrayMain [i] %26lt;%26lt; " ";
}
cout %26lt;%26lt; endl %26lt;%26lt; endl;
cout %26lt;%26lt; endl;
system("pause");
return 0;
};
void GetInputArray (int%26amp; userSize, int originalArray [])//Gets input array from user
{
cout %26lt;%26lt; "How many integers do you want to put into the array?: " %26lt;%26lt; endl;
cin %26gt;%26gt; userSize;
cout %26lt;%26lt; endl;
if (userSize %26gt; 0)
{
cout %26lt;%26lt; "Enter " %26lt;%26lt; userSize %26lt;%26lt; " integer(s) into the array (e.g. 5 88 4 etc):" %26lt;%26lt; endl;
for (int i = 0; i %26lt;= userSize - 1; i++)
{
cin %26gt;%26gt; originalArray [i];
}
cout %26lt;%26lt; endl;
}
else
{
cout %26lt;%26lt; "Invalid Entry!" %26lt;%26lt; endl;
}
}
void FindMinMax (int userSize, int%26amp; min, int%26amp; max, int originalArray [])
{
//find the mimimum number in the array
min = originalArray [0];
for (int i = 0; i %26lt;= userSize - 1; i++)
{
if (originalArray [i] %26lt;= min)
{
min = originalArray [i];
}
}
//find the maximum number in the array
max = originalArray [0];
for (int i = 0; i %26lt;= userSize - 1; i++)
{
if (originalArray [i] %26gt;= max)
{
max = originalArray [i];
}
}
}
void Sort (int userSize, int originalArray [], int lowSortedArray [], int highSortedArray [])
{
int j = (userSize - 1);
//copy contents of originalArray into lowSortedArray
for (int i = 0; i %26lt;= userSize - 1; i++)
{
lowSortedArray [i] = originalArray [i];
}
//sorts the lowSortedArray from low to high
sort (lowSortedArray, lowSortedArray + userSize);
//reverse copies the lowSortedArray to the highSortedArray (high to low sorted array)
for (int i = 0; i %26lt;= userSize - 1; i++)
{
if (j %26gt;= 0)
{
highSortedArray [j] = lowSortedArray [i];
j--;
}
}
}
void SplitArrayProcessing (int userSize, int originalArray [], int lowSplitArray [], int highSplitArray [])
{
int j = 0;
//splits the array in half
for (int i = 0; i %26lt;= userSize - 1; i++)
{
if (i %26lt; (userSize/2))
{
lowSplitArray [i] = originalArray [i];
}
else
{
highSplitArray [j] = originalArray [i];
j++;
}
}
}
Reply:// this is a simple example, but not necessarily the most efficient
int myArray[5] = {6,1,3,8,2}; // an array of 5 random integers
int min = myArray[0]; // use the first value in the array
int max = myArray[0]; // use the first value in the array
// the loop below compares min and max to each value in the array
for(int i = 1; i %26lt; 5; i++) // 5 is the size of the array
{
if(min %26gt; myArray[i]) min = myArray[i];
if(max %26lt; myArray[i]) max = myArray[i];
}
cout %26lt;%26lt; "MIN = " %26lt;%26lt; min %26lt;%26lt; endl; // prints the lowest number
cout %26lt;%26lt; "MAX = " %26lt;%26lt; max %26lt;%26lt; endl; // prints the highest number
Subscribe to:
Posts (Atom)