Hey guys
i have been working on a C++ program as a revision for an upcoming final exam.
One of the main point is to know how to create a function that would invert the elements in your array. That one has given me alot of trouble. A tip or a hint would be greatly appriciate it :D
ie: A={1,2,3} -------> A'={3,2,1}
Now i have tried using the fonction on the top of my program but it messed it up for me. and all my output was invalid.
That's the code i have so far and i am sorry in advance if it's difficult to read i have tried making it as esthetic as possible. -
#include<iostream>
-
using namespace std;
-
#define MAX 100
-
-
-
int sum( int number[], int n) // Sum fonction of the n elements of the array.
-
{
-
int z=0;
-
for( int i=0;i<n;i++)
-
z=z+number[i];
-
return (z) ;
-
}
-
-
-
-
-
-
-
int main()
-
{
-
int number[MAX];
-
int index;
-
int count=0;
-
char more;
-
-
-
-
do
-
{
-
cout<<"Enter a value: ";
-
cin>>number[count];
-
-
if (number[count]<0 || number[count]>100)
-
{
-
cout<<"You have entered a wrong value\n";
-
system("pause");
-
continue;
-
}
-
-
-
cout<<"Any more items to enter y/n: ";
-
cin>>more;
-
count++;
-
-
}
-
-
while (more!='n');
-
int n;
-
cout<<"Enter the number of elements you wish to sum up\n";
-
cin>>n;
-
-
-
cout<<"The sum is "<<sum(number,n)<<endl;
-
-
for (index=0; index<count;index++) // calling the elements of the array
-
{
-
cout<<endl;
-
cout<<"The CONTENT OF ARRAY ELEMENT "<< index <<" "<<"IS ";
-
cout<<number[index];
-
}
-
-
-
cout<<endl<<"This Array is composed of "<<count<<" Elements";
-
cout<<endl<<endl;
-
return (0);
-
}
-
Thanks for any help :)
3 1679
Updated code
/free bumb ;) still need help
Ganon11 3,652
Recognized Expert Specialist
How about this: - void invertArray(int[] anArray, int size);
-
// invertArray creates a new array, called temp, with the same size
-
// as anArray. Then, using a for loop going from size - 1 to 0, the
-
// function sets temp[size - the index - 1] to anArray[the index].
-
// Finally, the function sets anArray equal to temp with a simple
-
// for loop.
-
// Precondition: anArray contains x elements, and size is x.
-
// Postcondition: anArray contains the same values, but in reverse order.
How about this: - void invertArray(int[] anArray, int size);
-
// invertArray creates a new array, called temp, with the same size
-
// as anArray. Then, using a for loop going from size - 1 to 0, the
-
// function sets temp[size - the index - 1] to anArray[the index].
-
// Finally, the function sets anArray equal to temp with a simple
-
// for loop.
-
// Precondition: anArray contains x elements, and size is x.
-
// Postcondition: anArray contains the same values, but in reverse order.
Am not near a compiler at the moment but i think that should do it.
Just one question tho, i am creating a new array with the same size as my previous one right ?
And let's say i dont enter any value for my initial array how could i put a safety mechanism that won't call this function ?
Thanks for your help :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Nathan Rose |
last post by:
Here's my problem. I am reading from a text file using this:
if (!file_exists($file))
{
echo "Don't exist\n";
return false;
}
$fd = @fopen($file, 'r');
if (!is_resource($fd))
|
by: KellyH |
last post by:
Hi, I hope someone can point me in the right direction.
I'll get it out of the way: Yes, I am a college student. No, I am not
looking for anyone to do my homework, just looking for help. I have...
|
by: Tommy Lang |
last post by:
I am working on this project and I need some help/pointers/comments to
get me started, I am stuck.
The program will be used to store information in an array while it is
running.
I need to store...
|
by: Chad Z. Hower aka Kudzu |
last post by:
A few of you may recognize me from the recent posts I have made about Indy
<http://www.indyproject.org/indy.html>
Those of you coming to .net from the Delphi world know truly how unique and...
|
by: Thomas Connolly |
last post by:
Anyone know if there is a C# equivallent to:
enum
{
LIFFE_SIZE_AUTOMARKETREF = 15
};
typedef char LiffeAutoMarketReference ;
Thanks,
| |
by: vinod.bhavnani |
last post by:
Hello all,
I need desperate help
Here is the problem:
My problem today is with multidimensional arrays.
Lets say i have an array A this is a 4 dimensional
static array.
|
by: gcary |
last post by:
I am having trouble figuring out how to declare a pointer to an array
of structures and initializing the pointer with a value. I've looked
at older posts in this group, and tried a solution that...
|
by: SpreadTooThin |
last post by:
If you are deriving a new class from another class,
that you must (I assume) know the initializer of the other class.
So in myClass
import array
class myClass(arrary.array):
def...
|
by: javabeginner123 |
last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |