C++ :: Storing Integer Arrays Safely
Apr 22, 2013
I have an assignment where I have to design, implement, and test a class for storing integer arrays "safely". I do not know how to set up the destructor.
The goal of this programming assignment is to give students practice defining and using classes. In particular, students are required to design, implement, and test a class for storing integer arrays "safely". The array should be able to hold any number of integers up to 100.
In the class header file "SafeArray.h" students must define the class and specify the constructor/destructor functions, the public methods and private variables. In the class implementation file "SafeArray.cpp" students must implement the following operations:
constructor - to initialize the object.
copy constructor - to copy an object.
destructor - to delete the object.
set - allow the user to set a value of the array at a particular location.
get - allow the user to get a value of the array at a particular location.
print - print out the array.
add - add the elements of one array to another.
subtract - subtract the elements of one array from another.
The purpose of your main program "main.cpp" is to demonstrate that all of the methods above work properly. You should have at least one call to each of the methods, and print out the array as needed to show that the operations are performing correctly.
"SafeArray.h":
#ifndef SAFEARRAY_H
#defineSAFEARRAY_H
class Safe {
private:
// Declare variables to store A, B and C
[Code] ....
View 2 Replies
ADVERTISEMENT
Apr 23, 2013
I am currently stuck on what I should do next in a program I am working on. These are my instructions:
Design, implement, and test a class for storing integer arrays "safely". The array should be able to hold any number of integers up to 100.
In the class header file "SafeArray.h" students must define the class and specify the constructor/destructor functions, the public methods and private variables. In the class implementation file "SafeArray.cpp" students must implement the following operations:
constructor - to initialize the object.
copy constructor - to copy an object.
destructor - to delete the object.
set - allow the user to set a value of the array at a particular location.
get - allow the user to get a value of the array at a particular location.
print - print out the array.
add - add the elements of one array to another.
subtract - subtract the elements of one array from another.
The output of my program is suppose to look like this:
Set q1: 2, 3, 4
Print q1: 2, 3, 4
Set q2: 1, 4, -2
Print q2: 1, 4, -2
Add q2 to q1
Print q1: 3, 7, 2
Get q1 at 1: 7
Here is the code I have so far.
*main.cpp*
#include <iostream>
#include "SafeArray.h"
using namespace std;
int main() {
// Declare a SafeArray object
Safe obj;
[Code] ....
View 1 Replies
View Related
Oct 25, 2013
I am trying to assign the integer value to unsigned char array. But it is not storing the integer values. It prints the ascii values. Here the code snippet
Code: uchar uc[100];
for(i=0;i<100;i++)
{
uc[i] = i;
}
The values which are stored in uc[] is ascii values.I need the integer values to be stored in uc[]. I tried to do it with sprintf. but the output is not as expected. if I print the uc[i] it should diplay the value as 0,1,2....99.
View 9 Replies
View Related
Oct 3, 2013
So I'm supposed to write a code that asks a user for a string and then displays the hex, decimal, and binary code for each individual letter and then tells the user how many bits in binary were 1. For example: Enter a line of text: Hello
The ASCII code for 'H' is 0x48 in hex, 72 in decimal, or 01001000 in binary, 2 bits were set.
The ASCII code for 'e' is 0x65 in hex, 101 in decimal, or 01100101 in binary, 4 bits were set.
The ASCII code for 'l' is 0x6c in hex, 108 in decimal, or 01101100 in binary, 4 bits were set.
The ASCII code for 'l' is 0x6c in hex, 108 in decimal, or 01101100 in binary, 4 bits were set.
The ASCII code for 'o' is 0x6f in hex, 111 in decimal, or 01101111 in binary, 6 bits were set.
So far I've got a code that will display the binary bit pattern by shifting a mask and testing for a 1 or 0. The problem is I can't figure out how to make it so the 1's and 0's get put into a single integer rather than just printing out. I hope that makes sense. Here's my whole code.
Code:
#include<stdio.h>
main ()
{
int i;
char input;
printf ("Enter ........: ");
scanf ("%c", &input);
for (i = 1; i <= 8; i++)
[Code]...
View 9 Replies
View Related
Dec 6, 2014
I am writing trying to store a list of numbers into an integer as bits. So far I have this:
n=n & 0x0f
integer = integer<<2
integer = integer | n;
convert(integer));
I want it to store the integers as bits so that I am move them over and store them farther down the row as more numbers are added, but instead each new number is being added to the previous and I'm just getting a larger integer. Is this even a feasible way to store integers within an integer?
View 1 Replies
View Related
Mar 29, 2014
I am doing this program where I analyze colors in a ppm file. Now I am having a hard time storing the data into arrays in this order...
P3
600 339
255
44 5 8
44 5 8
43 4 7
42 3 6
42 3 4
44 5 6
...
So far i tried using fscanf()
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#define DEBUG true
#define CHECK true
[Code] ....
I then realized that the order that the second method gave me will make it very hard for me to calculate the RGBs. Because they will be calculated like wise..
P3
600 339
255
44 5 8 = sum
44 5 8 = sum
43 4 7 = sum
42 3 6 = sum
42 3 4 = sum
44 5 6 = sum
...
but that isn't the issue right now..
View 10 Replies
View Related
Feb 25, 2014
I'm am trying to get info from a file and put it into three arrays but it isn't working.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void sort(ifstream& infile){
[code]....
View 3 Replies
View Related
Nov 18, 2013
I am trying to create a program that will give me an value for a chosen from the user array ut I believe the program I've made does not recognize the values of the previous arrays. (Here is my program):
#include<stdio.h>
int main() {
int n;
int i;
int j;
float c;
float a[10000];
[Code] ....
There must be problem cause every value I give n(only for n=1 the answer is correct) the result is "a[n] is -inf"
View 1 Replies
View Related
Feb 7, 2014
I'm trying to create a function where it allows the user to type in multiple amounts of integers, so if the user wanted to have 3 different storages that hold different integers, the input would look something like this:
5
97 12 31 2 1 //let's say this is held in variable "a"
1 3 284 3 8 // "b"
2 3 482 3 4 // "c"
2 3 4 2 3 // "d"
99 0 2 3 42 // "e"
Since we don't know what number the user will input every time, I'm not sure how to create a dynamically allocated array that will create an x amount of arrays every time.. I want to be able to access each index of a, b, c, d, e or however many arrays there are.
So far, this is what I have, but I'm having trouble creating the arrays since it's unpredictable. I'm purposely not using vectors because I don't really get how pointers work so I'm trying to play around with it.
int* x;
int length, numbers;
cin >> length;
x = new int[length]
for (int i=0;i<length;i++)
{
cin >> numbers; //this doesn't work because it only grabs the first line for some reason
x[i] = numbers
}
View 3 Replies
View Related
May 7, 2014
I would like to be able to safely eject a USB drive (in Windows) when user selects that drive in a listbox and clicks the Eject button in an MFC dialog app. Any functions or (link) that would be up to date (i.e. compatible with Windows 8)?
View 1 Replies
View Related
Jan 9, 2014
MSDN says this about EnumProcess. If PSAPI_VERSION is 2 or greater, this function is defined as K32EnumProcesses in Psapi.h and exported in Kernel32.lib and Kernel32.dll. If PSAPI_VERSION is 1, this function is defined as EnumProcesses in Psapi.h and exported in Psapi.lib and Psapi.dll as a wrapper that calls K32EnumProcesses.
Programs that must run on earlier versions of Windows as well as Windows 7 and later versions should always call this function as EnumProcesses. To ensure correct resolution of symbols, add Psapi.lib to the TARGETLIBS macro and compile the program with DPSAPI_VERSION=1. To use run-time dynamic linking, load Psapi.dll.
code to safely use this method that will safely run on older and newer OS?
View 5 Replies
View Related
Nov 3, 2014
So first I have to display a 2D array with all 0s, which is pretty easy.
#include <iostream>
using namespace std;
int main (){
int array[5][5];
for(int a=0; a<5; a++){
for(int b=0; b<5; b++){
array[a][b] = 0;
[Code] ....
So this displays
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Next, this is where it gets confusing. I have to create a virtual creature by storing a letter into a random position in the array (the array can be up to 20x20 in size). Then make a function that searches the array for creatures, so it would search for that character. When it finds a creature, it should randomly decide to either move the creature to an adjacent position, or have it stay where it is. After, it should ask the user to create a new creature, or quit.
So how would I go about adding & modifying the current code to achieve what is listed above?
View 2 Replies
View Related
Dec 1, 2014
i'm having difficulty with a problem, i need to add two big numbers suchas 54646774456776 and another one 445556777554 and it would print the result. how can i approach this problem without the use of arrays?
View 4 Replies
View Related
Mar 27, 2014
I have become overwhelmed an frustrated at these current operator overloads!
Currently I still can not get my +,* or istream operators working at all!
#include <iostream>
#include <cstring>
#include "myint.h"
[Code]....
View 1 Replies
View Related
Apr 24, 2013
Ok so I am currently trying to add and subtract two sets of integer arrays. When I run my program, the program does not subtract the numbers from the first set with the numbers from the second set. Could anyone here take a look at my code and help me figure out what the problem is?
#include <iostream>
#include "SafeArray.h"
using namespace std;
[Code].....
View 1 Replies
View Related
Apr 23, 2013
I am currently working on a program that uses a class to store integer arrays. I have most of the code done, but I am having trouble on the main.cpp part of my program. The program should display this:
OBJ 1:
2 3 4
OBJ 2:
1 4 -2
Addition:
OBJ 1 =
3 7 6
Subtraction:
OBJ 1 = 1 -1 6
But when I run and compile my program, this is what I get:
OBJ 1:
2 3 4
OBJ 2:
ADDING:
OBJ 1 =
4 6 8
Subtraction:
OBJ 1 =
Here is my current main.cpp:
Code:
#include <iostream>
#include "SafeArray.h"
using namespace std;
[Code].....
View 3 Replies
View Related
Jun 21, 2013
Passing and returning character and integer arrays with functions using simple programs...use pointers if necessary
View 1 Replies
View Related
Jun 15, 2014
changing a 9 digit integer into a new 9 digit integer through simple mathematical operations. For example, I need to change 123456789 into the new digit 456123789. Sometimes I need to change a 9 digit integer into an 8 digit integer. An example is 789062456 into 62789456. I can ONLY use simple mathematical operations (addition, subtraction, multiplication, division and modulo).
View 4 Replies
View Related
Oct 15, 2014
Complete the function myitohex so that its converts 8-byte integer to string based hexadecimals.
Ex: printf("0x%s",myitohex(64,hex)); should printout "0x40" on the screen
char *myitohex(uint8_t i, char *result){
???
return result;
}
I wrote:
#include <stdio.h>
#include <stdint.h>
char *myitohex(uint8_t i, char *result){
*result = i + '0';
[Code] ....
0xp gets printed out which is wrong. I think *result = i + '0'; is wrong. What changes should i do on this row ?
View 4 Replies
View Related
Feb 6, 2014
I define "Comwords" as a string, but apparently it takes the members as chars, then I can't set strings in a structure equal to the chars.
I see to also be having unknown problems with the ComMAL array and loading it values into another element of the same structure.
How to correct this? I was thinking of casting char elements as strings, but could find no reference in my library book regarding how to do that (lots on casting int's a doubles...)
Code:
int _tmain(int argc, _TCHAR* argv[]) {
int comm = 10;
int targ = 5;
int death;
struct AI_WORDS
[Code]....
View 2 Replies
View Related
Jul 1, 2014
Using a for loop, construct two 100 element arrays, x and y, such that element i of x stores the value sin(2*pi*i/100)) and the corresponding element of y stores cos((2*pi*i/100)). Print the values stored in the elements of x and y as you calculate them.
I have attempted to solve it but I'm not sure why the value 0 is only being printed, maybe I haven't assigned sin(2i/100)) and cos((2i/100)) to the arrays properly?
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main () {
[Code] .....
View 3 Replies
View Related
Dec 17, 2014
int arr[4][5];
for (int(&row)[5] : arr){
for (int &column : row){
} }
Why do we name it row instead column?
int arr[row][column]
Wouldn't this be like
int arr[4][5];
for (int(&column)[5] : arr){
for (int &row: column){
}
It just seems funny to me because we use row[5] which should be 4 if we meant row? Or did I read it wrong in C++ prime !?
View 2 Replies
View Related
Mar 26, 2013
I have a project to do in C and coming form Java I miss all the included features that are missing from C! I need to be able to store key value pairs in an quick and memory efficient manner. I've looked up using hash maps but I'm very new to C so don't really understand even the basic ones. I've looked at using a multi-dimensional array as I'm more comfortable with arrays but I'm unsure if that would count as a memory efficient and quick method?
View 14 Replies
View Related
Feb 5, 2014
So i'm creating bank system. Where I have function that reads the text file (balance), and I make a deposit, which adds what was in the balance = deposit + balance.
But this is what is happening. In my text file i have number 10. Function balance works fine and reads 10 from the text file.
When I make a deposit add a value to the balance, if i add 7, the new balance is 17. And i check the text file shows me is 17. Which is correct.
The problem is here. If i make another deposit, without closing the program, for example i add 5 to the balance, the new balance should be 22 = 17(balance) + 5(deposit), because 17 was store on the text file and it was the balance that was store on the text file on the last time. But it shows me 15, it adds the balance that the program first started which was 10(balance) + 5(deposit), but should had had be 17 + 5.
When I close the program the value on the text file, is 15, that was the last sum that i did.
int main()
{
double balance = 0;
balance = currentBalance(balance);
menu(balance);
[Code].....
View 18 Replies
View Related
Dec 9, 2014
I wanted to find all the prime until a specified limit in C. I use the Sieve of Eratosthenes. But when I define the limit to anything more than a 7 digit number the program terminates.
Code:
#include<stdio.h>
#define limit 1000000000
int main(void)
{
unsigned long long int i,j;
int primes[limit] = {0};
//int count =0;
for(i=2;i<limit;i++)
}
[code]....
I believe that this might be because the size cannot be declared array cannot be more than the a 7 digit number. I think so. how to store a 10 digit number in C?And can't unsigned long long hold a 10 digit?
View 1 Replies
View Related
Mar 27, 2013
I am having trouble storing strings into an array. Basically I have a console program where it has an option. This is just an example;
Code:
char name[max];
char listofnames[maxname];
int counter;
....
printf("Add names
");
And how can I print it?
View 9 Replies
View Related