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 1683
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 been
reading this ng all semester, and found it very helpful. I just have a
stupid problem going on right now.
Here's the project:
We have to...
|
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 objects of my classes Person(superclass),
Student(inherit Person), Teacher(inherit Person) in that array.
The name will be the unique key. These...
|
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
"huge" Indy is both as a project, in support, development, and use. But
Indy is new to the .net world.
Indy is a HUGE library implementing over 120...
|
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 looked
sensible, but it didn't work right. Here is a simple example of what
I'm trying to accomplish:
// I have a hardware peripheral that I'm...
|
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 __init__(self, now here I need to put array's constructor
parameters..., then mine):
array.array.__init__(self, typecode)
|
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 the description:
The monsters are of a very strange kind, called "Bigmon". They have some basic characteristics, like attack and defense power, life...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |