Dynamic array (with negative indeces) (2024)

cphipps (37)

OK, everything looks alright to me, but I'm pretty green when it comes to this. It is compiling and running fine, but it isn't outputting what I am expecting. Maybe I'm missing something? Thanks ahead of time for any help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>using namespace std;int main(){int arraySize;int negValue;int counter;int counter2 = 0;cout << "Please input the size of the array you want: ";cin >> arraySize;int negIndexArray[arraySize];cout << "Please input how far you want to go into negative in indeces: ";cin >> negValue;int* point = negIndexArray+negValue;//index into the arrayfor (counter = negValue; counter<arraySize && counter>negValue; counter++)point[counter] = counter2;counter2++;cout << point[negValue];cout << point[arraySize];return 0;}

LB (13399)

Line 14 is illegal and should not compile.

Also, negative indicies are the memory before the array, which is undefined behavior to access. Most likely you are overwriting other variables.

Well it compiles and runs fine...I know negative indices are memory before the array. I know it can be done though, it's an assignment in my book. Maybe I'm just going about it the wrong way?

LB (13399)

cphipps wrote:
Well it compiles and runs fine...

That's no indication of valid code. There are plenty of invalid programs that "compile and run fine" on some compilers.

Could you quote the exercise specifically? I have a feeling you're misunderstanding it (or maybe it is trying to show you what memory corruption can cause).

cphipps (37)

I know this also, I'm green, but it isn't my first rodeo. I was saying that because you said it shouldn't compile.

Here is the assignment though:

Recall that in C++, there is no check on an array index out of bounds. However, during program execution, an array index out of bounds can cause serious problems. Also, in C++, the array index starts at 0. Design and implement the class myArray that solves the array index out of bounds problem and also allows the user to begin the array index starting at any integer, positive or negative. Every object of type myArray is an array of type int. During execution, when accessing an array component, if the index is out of bounds, the program must terminate with an appro-priate error message. Consider the following statements: myArray<int> list(5); //Line 1 myArray<int> myList(2, 13); //Line 2 myArray<int> yourList(-5, 9); //Line 3 The statement in Line 1 declares list to be an array of 5 components, the component type is int, and the components are: list[0], list[1], ..., list[4]; the statement in Line 2 declares myList to be an array of 11 com-ponents, the component type is int, and the components are: myList[2], myList[3], ..., myList[12]; the statement in Line 3 declares yourList to be an array of 14 components, the component type is int, and the components are: yourList[-5], yourList[-4], ..., yourList[0], ..., yourList[8]. Write a program to test the class myArray.

LB (13399)

Ah, the assignment want you to make a class that allows for a different starting index than 0 and to disallow accessing out-of-bounds locations.

cphipps (37)

Yes...I guess I should've been more specific for the purpose of my program. I didn't quite fully understand it myself though. I have completely abandoned the above code though, and have started another way. I think I have it working fine, at the moment at least. Thanks for the assistance!

cphipps (37)

Edit:
False Alarm! I have figured it out! There were several errors just for the record. I just had to walk away for a bit.

OK, I abandoned my old code, and have started fresh.
Here is the error I'm getting:
114G:\USB Files\USB Files\Spring 2014\C++ Homework\Session 10\myArrayHeader.h[Error] expected unqualified-id before '{' token

Anyone see what I'm missing?
Thanks for any help!

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>#include <cstdlib>#include "myArrayHeader.h"int main(){intArray mainArray;int i=0;std::cout<<"Type the index of the array element you want to see or 1234 to stop the program: "<<std::endl;while (i!=1234){ std::cin>>i; std::cout << mainArray[i] << std::endl;}return 0;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef myArrayHeader_H#define myArrayHeader_H#include <iostream>#include <cstdlib>//int listArray[10];class intArray{public:intArray();int listArray[10];{int *negList;negList = list;*(negList-5) = 2;*(negList-4) = 5;*(negList-3) = 4;*(negList-2) = 1;*(negList-1) = 0;*(negList+0) = 1;*(negList+1) = 6;*(negList+2) = 8;*(negList+3) = 9;*(negList+4) = 1;}int &operator[](int i);};#endif/*// Provide range checking for intArray.int &intArray::operator[](int i){if(i<0 || i> 9) {cout << "Boundary Error\n";system ("pause");exit(1);}return list[i];}*/
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>#include "myArrayHeader.h"using namespace std;int &intArray::operator[](int i){if(i<0 || i> 9) {cout << "Error: Out of bounds!\n";exit(1);}return list[i];}

Last edited on

LB (13399)

WHat are lines 11 to 24 supposed to be for in your header? They're not attached to anything and they won't work anyway.

Dynamic array (with negative indeces) (2024)
Top Articles
McFarlane Toys Digital Archives - McFarlane Toys
Batman by Todd McFarlane with McFarlane Toys Digital Collectible - McFarlane Toys
Mybranch Becu
Lexi Vonn
Danatar Gym
Amtrust Bank Cd Rates
Miles City Montana Craigslist
Parks in Wien gesperrt
Mr Tire Rockland Maine
Ap Chem Unit 8 Progress Check Mcq
Tokioof
What Is Njvpdi
Washington, D.C. - Capital, Founding, Monumental
24 Hour Walmart Detroit Mi
Www Craigslist Com Phx
Toy Story 3 Animation Screencaps
Energy Healing Conference Utah
Joann Ally Employee Portal
Ubg98.Github.io Unblocked
Aldi Bruce B Downs
Free Personals Like Craigslist Nh
Sef2 Lewis Structure
Churchill Downs Racing Entries
Garden Grove Classlink
Jackass Golf Cart Gif
Cvs Sport Physicals
Rogold Extension
Pch Sunken Treasures
Verizon TV and Internet Packages
Bozjan Platinum Coins
Tra.mypatients Folio
Skroch Funeral Home
Jennifer Reimold Ex Husband Scott Porter
Dallas City Council Agenda
Craigslist Mount Pocono
20+ Best Things To Do In Oceanside California
Hell's Kitchen Valley Center Photos Menu
How to Draw a Sailboat: 7 Steps (with Pictures) - wikiHow
Ursula Creed Datasheet
Tugboat Information
One Main Branch Locator
Lonely Wife Dating Club בקורות וחוות דעת משתמשים 2021
Atom Tickets – Buy Movie Tickets, Invite Friends, Skip Lines
Best GoMovies Alternatives
ACTUALIZACIÓN #8.1.0 DE BATTLEFIELD 2042
13 Fun &amp; Best Things to Do in Hurricane, Utah
Chase Bank Zip Code
Reilly Auto Parts Store Hours
Jimmy John's Near Me Open
Contico Tuff Box Replacement Locks
Evil Dead Rise - Everything You Need To Know
Sdn Dds
Latest Posts
Article information

Author: Fr. Dewey Fisher

Last Updated:

Views: 5705

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Fr. Dewey Fisher

Birthday: 1993-03-26

Address: 917 Hyun Views, Rogahnmouth, KY 91013-8827

Phone: +5938540192553

Job: Administration Developer

Hobby: Embroidery, Horseback riding, Juggling, Urban exploration, Skiing, Cycling, Handball

Introduction: My name is Fr. Dewey Fisher, I am a powerful, open, faithful, combative, spotless, faithful, fair person who loves writing and wants to share my knowledge and understanding with you.