
|
|
[
Working with arrays example ::
Exercise #9 ]

|
Initialize array (at compile time):
int myarray[3] = {4, 19, 2};

Print Array on screen
/* Print the passed array on the screen
* @return : void
*/
void printArray(int myarray[])
{
cout << "Array: [";
for (int i=0; i< SIZE; i++)
{
cout << myarray[i];
if (i!=SIZE-1)
{ cout << ", "; }
}
cout << "]\n";
}

More on Arrays
/* returns true if the given string is a real number */
bool isReal(string instring)
{
int size = instring.size();
int dotcount = 0;
char c;
for (int i=0; i<size; i++)
{
c = instring[i];
if (c=='.')
{ dotcount++; }
else if (!isDigit(c))
{ return false; }
}
/* check if this number has more than 1 dots '.' */
if (dotcount<=1)
{ return true; }
else
{ return false; }
}
|
Top

|
New! - Download lab10.exe (win32) here and see how your program should correspond to various inputs.
See Solution - C++ solution here (will be available on Saturday since labs are running until friday)
Write a program that deals with arrays. Declare an array of 10 integers and fill this array with values at compile-time. Now shift the values in the array to the right (circularly) by a user specified amount. Print the array before and after the shifting.
For example, if the array held the following values: 3, 17, 2, 9, 12, 8, 43, 100, 55, 6 and the user wanted to shift the values to the right by 3, then the array would end up with the following values: 100, 55, 6, 3, 17, 2, 9, 12, 8, 43
To initialize an array at compile-time, on the same statement as the declaration, you will put the values, comma separated within curly braces.
int myarray[3] = {4, 19, 2};
Hint: Try to optimize the shifting process!. Your program should instantly (in very short period) respond to a requested shift of 1000000000. The optimization is only 1 line and is something that you already should know!
Notice: Be sure to put all your information (login, lab section, …) at the top of your program. Your lab programs DO NOT get turned in electronically. I will come around and check you off individually.
|
|