C++ :: Converting From Class Array To Int

Mar 24, 2014

Version 1 (works fine)

Creating array in main and then calling a sorting function from sorting class.

In main:

const size_t SIZE = 100;
int *array = new int [SIZE];
//fill array with ints, not shown here
quickSort(array, SIZE); //calling the sorting function in the sorting class

In the sorting class, quickSort is declared as such:

void quickSort(int arr[], int num);

Everything works great.

Version 2 (issues)

Instead of creating the array in main, I have set up a class for the array, MyArrayClass, where I create an object containing the array of ints. So far so good. The issues come when I write a member function to call quickSort. Even though my MyArrayClass object contains an array of ints, the code calling for quickSort() won't compile as the data type isn't ints but MyArrayClass (which in turn holds ints though). The compiler (using VS 2013 btw) complains that quickSort can’t convert the first argument from 'const MyArrayClass' to 'int[]'.

How do I cast my class object array of ints as an int[] in order to be able to call the quickSort function? Or should I solve this issue in some other way? I tried altering the sorting function to accept the object as it is, but that only created an avalanche of new errors, so thinking that converting/casting the object array --> int[] might be easier...

View 6 Replies


ADVERTISEMENT

C++ :: Converting Object To Different Class

Jun 22, 2014

Sandy is of class Person, who converts to Muslim and takes on a new name Fatima, and has all the attributes of Sandy, with new Muslim attributes (e.g. religion == Islam, etc..). At this point, Sandy can be deleted, and Fatima, now of class Muslim will play Sandy's role henceforth. The problem is that due to her new address, all the people who knew Sandy does not know Fatima. Manually changing Sandy's address to Fatima's address for all those people who knew Sandy is clearly not an acceptable method. I looked through all the design patterns and cannot find one to solve this problem. how to improve the design? Here's my simplified code showing the problem:

#include <iostream>
#include <string>
#include <typeinfo>
class Person {
std::string name;
Person* bestFriend;

[Code]...

Output:

sandy = 0x32658, 6Person
Mary's best friend is Sandy.
fatima = 0x23fec0, 6Muslim
Mary's best friend is Sandy.

Of course, we want to have: Mary's best friend is Fatima,. with mary->getBestFriend()->getReligion() == Islam, etc... How to redesign the whole thing so that this is automated (assume there are thousands of people who know her)?

View 14 Replies View Related

C++ :: Fraction Class - Warning Converting To Int From Double

Jul 28, 2013

I am trying to write a Fraction class and getting the following warning when compiling my code :

Fraction.cpp: In constructor 'Fraction::Fraction(double)':
Fraction.cpp:8: warning :converting to 'int' from 'double'

My Fraction.cpp class looks like :

#include "Fraction.h"
Fraction::Fraction(int n, int d):num(n),den(d) {
cout << This is double param constructor <<endl;
}
Fraction::Fraction(double d):num(d),den(0)

[Code] ....

How can I get rid of the warning ?

View 8 Replies View Related

Visual C++ :: Converting Program From Struct To Class

Sep 20, 2014

I am not exactly sure how to do this and i keep running into problems. This is my code here that works.

Code:
#include<iostream>
using namespace std;
struct record {
double quiz1;
double quiz2;
double midyear, midyear_one;

[Code] .....

View 5 Replies View Related

C++ :: Class To Store Nodes Of Tree - Converting Between Types

Jun 4, 2013

I'm writing a class to store a tree I have a small issue.

I have a class which stores a node of the tree, something like this:

template<class T>
class node {
private:
(stuff regarding parent node and child nodes);
T __data;
public:
// more stuff (constructors and utility functions) here
};

And I want to I want to be able to do the conversion T(node<T>) implicitly. For example, I want to be able to do this:

node<int> myNode;
myNode=5;
myNode+=10;
myNode*=9;
int x=int(myNode/4);
cout << x+myNode;

And this:

class myClass {
int x;
int y;
void print () {cout << x << ' ' << y << endl;}

[Code] ....

How could I achieve this?

View 2 Replies View Related

C++ :: Converting 1D Array To 2D Matrix?

Jan 12, 2015

Q.Write a program using user-defined function which accepts an integer array and its size as arguments and assign the elements into a two dimensional array of integers in the following format: If the array is 1,2,3,4,5,6, the resultant 2D array is

1 2 3 4 5 6
1 2 3 4 5 0
1 2 3 4 0 0
1 2 3 0 0 0
1 2 0 0 0 0
1 0 0 0 0 0

Ans.

#include<iostream>
using namespace std;
int two_print(int x, int one[999]) {
int two[999][999], q=x, z=x;
for(int k=0; k<x; k++) {
q--;

[code].....

View 2 Replies View Related

C/C++ :: Converting 1D String Array To 2D

Mar 27, 2015

I am trying to convert a 1d string array to a 2d but i couldnt do it. Heres my code so far,

for(i=0;str2d[i][j]='';i++)
{
for(j=0;str2d[i][j]=' ';j++)
{
str2d[i][j] = str1d[k];
k++;
}
}

str1d is a paragraph btw.I tried using NULL instead of '' but didnt work either.

View 5 Replies View Related

C++ :: Converting String To Char Array

Mar 30, 2014

In this program, I have to ask the user for an employee, then the program will check to see if the file for that employee exist, if it doesnt then it will automatically create the file.

ReadNew function reads the file....check to see if it exist

CreateNew function creates a new file.

In my code I have no problem with the first part of reading file.. and my createnew function works in other programs where I am asking for input of file name to create the file name. However in this code I cannot figure how to automatically pass the input filename from the ReadNew function to the CreateNew function. I can't ask the user to enter the name a second time, so I have to pass the input filename into both functions. Here is my code.

Code:

//Create a file, append to it, and read it.
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;
char filename[256];
string n;
string filelist;
void CreateNew(ofstream & FileNew);

[Code]...

View 1 Replies View Related

C :: Converting Function Data To Array

Apr 4, 2013

How can I change this function so it stores each of the even integers into an array of integers?

Code:
void sumIntegers ()
{
int num = 0;
int i = 0;

[Code]....

View 1 Replies View Related

C++ :: Converting String To Integer Array

Apr 17, 2014

For Example, it the entered string is: 0324152397 I want it to get stored in an array like-[0] [3] ...[7]. Secondly the string entered may be of any length that is defined only at run time. So, I also need to calculate string length. How could I do that.

View 2 Replies View Related

C++ :: Converting String Into Double Array

May 18, 2014

I am having a lot of trouble extracting values from a string into a double array.

I have a string that looks something like this: "asdf 1.320 1 234.43 5.00000 3"

All I want to do is extract the doubles and store them in an array of doubles.

View 8 Replies View Related

C++ :: Typecasting Char Array To Int (Not Converting)

Jun 1, 2014

I am looking for the correct syntax to typecast a character array as an int. So I would be able to do something like this:

char ch[4];
//...Read characters from file into ch or something, etc...
int i = ( int ) &ch;

or

char ch[4];
//...Read characters from file into ch or something, etc...
int i;
strncpy ( ( char* ) &i, ch, 4 )

I want all the 4 characters ( or bytes ) of ch to be classed as an int. So we are taking the 4 bytes of the character array and placing them in 4 bytes of the integer? How would i go about this?

View 2 Replies View Related

C/C++ :: Converting 2D Array Code To Malloc

Mar 2, 2015

I made a program that adds two matrices and displays their sum with a max dimension of 100.

/* This program asks the user for 2 matrices called A and B, as integers,
and displays their sum, C. The max dimension of each matrix is 100. */

#include <stdio.h>
// Construct function
void construct() {
int m, n, i, j; // Variables
int first[100][100], second[100][100], sum[100][100]; // Matrices variables

[Code] ....

Now I need to change it so there is no max size for each matrix. The arrays will be larger than 100x100 so I need to use malloc to create my arrays. So I cant use int A[rows][cols]. This is what I did to covert arrays to malloc. It compiles but it crashes after I entered all the integers.

/* This program asks the user for 2 matrices called A and B, as integers,
and displays their sum, C. The max dimension of each matrix is 100. */

#include <stdio.h>
#include <stdlib.h>
// Construct function
void construct() {
int m, n, i, j; // Variables
int *first = NULL;

[Code] .....

View 8 Replies View Related

C :: Converting Char Array To Int16 Type

Apr 18, 2013

I am trying to convert a char buffer[1024] with raw data to int 16 buffer2[1024] which is supposed to have hex data (0x01, 0x02 kind of format).

Code:
char temp_buffer[1024];
block_size = sizeof(temp_buffer);
num_blocks = sizeof(char);
Status = mount(0, &myfsObject);
if(Status != DAVEApp_SUCCESS)

[Code] .....

I am verifying the data of resultant buffer by creating a new file and writing data to it. The result which i got is:

Code: H uy H uy H uy H uy H uy H uy H uy H uy

View 2 Replies View Related

C++ :: Simply Converting Character Array To A Short Int

Feb 14, 2013

Any function of simply converting character array to a short int?For example. char array[4]={'0','2','f','f'}; to short int 767?

View 3 Replies View Related

C Sharp :: Test Enum Without Converting To Array First?

May 7, 2013

Is there a quick way to determine if a value is in an enum? Consider for example,

public enum GenderEnum
{
Unknown = 0,

[Code].....

I don't want to convert to an array if possible, just check the enum and get a yes/no answer.

View 1 Replies View Related

C :: Converting A String Array Into Uppercase Letters From File

Apr 19, 2013

I am new to coding Here is the problem. Have a program prompt the user for a filename to open. Change every alphabetic character in the file to a capital letter. Numbers and special characters should not be changed. Print the output to the screen.

Here is my code so far but i am only returning the last line of text capitalized from the file. I am trying to get the program to display all of the three lines of text from the file capitalized. The program displays the file correctly before i try and convert everything toupper();

Code:

Code: #include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
int main()
{
char line[81], filename[21];
int i;
FILE *inFile;

[Code]...

View 6 Replies View Related

C/C++ :: Converting String To Upper Case Via Dynamic Array?

May 21, 2014

I am trying to understand why i keep getting errors in my code. The errors are after the string is converted in my console window. I have to allocate and delete memory via dynamic array to do the problem.

#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main() {
string sentence;
int size;

[code]....

I just want to know why the extra characters are at the end of my conversion and how to make them stop.

View 3 Replies View Related

Visual C++ :: Display Array Of Bytes Without Converting To HBITMAP?

Jun 2, 2013

I followed a tutorial to load a bitmap from file and convert it to a array of bytes to access it directly. Now to display the bitmap on screen do I have to convert it to a HBITMAP or can I display it from the array of bytes?

Also the program I am building is a calendar program and I need to know if I can manipulate the bitmaps pixels in the byte array (e.g change color and resize image)

here is the code to load the bitmap

HTML Code:

BYTE* Sprite2::LoadBMP(int* width, int* height, long* size, LPCTSTR bmpfile ) {
BITMAPFILEHEADER bmpheader;
BITMAPINFOHEADER bmpinfo;
DWORD bytesread;
HANDLE file = CreateFile(bmpfile,GENERIC_READ, FILE_SHARE_READ,
0, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0);

[code]....

View 6 Replies View Related

C++ :: Modify Contents Of 1D Character Array - Converting To Upper Case

Feb 10, 2015

I am unsure how to write a function which modifies the content of the 1D character array and puts all of the letter it contains into uppercase. the following are the letters which i am trying to convert.

char text[MAX+1] = {'T', 'e', 's', 't', 'e', 'r', EOT};

The output to this should look like T E S T E R EOT

View 2 Replies View Related

C++ :: Deleting Array Of Derived Class Objects Through Base Class Pointer

Mar 21, 2015

In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,

Code:
class B
{
public:
B():_y(1){}
virtual ~B() {
cout<<"~B()"<<endl;

[Code] ....

This sample code does exactly what I want. So does the author mean the way I did is undefined?

View 1 Replies View Related

C++ :: Creating Array Of Pointers To Base Class To Point To Derived Class Objects Dynamically

Jan 16, 2013

Please consider the following code :

#include <iostream>
using namespace std;
class superclass;
class subclass1;
class subclass2;

[Code] ....

As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.

View 2 Replies View Related

C++ :: Exchange Array Between Derived Class And Base Class?

Apr 8, 2014

Base class has an array, whose size is controlled by the derived class.

I can't use the STL and use a 2003 compiler, so things like std::vector and std::array are out. I also can't use dynamic memory allocation.

So I thought of a few options:

1.
template <int N> class myBaseClass { ... int array[N]; ... }

then class MyClass: public myBaseClass<8> ... etc ...

2.
have a int **array in the base and assign in the derived class.

3.
give the base some virtual methods such as int *getArray or even int &getInt for more safety.

View 6 Replies View Related

C :: Converting One Dimensional Array To Two Dimensional Array

Aug 30, 2013

convert an one dimensional array into a two dimensional array and print like a matrix.

input: 34 50 2 4 90 33 7 80 9
output: A is a 3x3 matrix
34 50 2
4 90 33
7 80 9

View 12 Replies View Related

C++ :: Converting One-dimensional To Two-dimensional Array

Jan 17, 2014

I had a hard question in my C++ final exam and I'm trying to solve it for the last 3 days. I haven't succeded yet! Here is the question: You have a one-dimensional array A[20]={1,2,3,4,...,20} and B[5][4] you have to assign the A array's elements to the B array but there is an order which is: B[5][4] = { { 12, 9, 11, 10 }, { 14, 7, 13, 8 }, { 16, 5, 15, 6 }, { 18, 3, 17, 4 }, { 20, 1, 19, 2 } } and there is a restriction: you can only use ONE for statement, nothing else!

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int A[20] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20 }; // define A array's elements.
int B[5][4] = { 0 }, k = 1; // define B array and k counter.

[code]....

I can't narrow the statements to one,This program works perfectly but it shouldn't be that long, we need ONLY ONE FOR statement, not two!

View 2 Replies View Related

C++ :: Storing Class In Array

May 2, 2013

How do I go about storing info to an array of class?I need to do something like this:

info[count] = ReadNameRating(n, r);

Where info is an array of my own class and ReadNameRating reads the name and rating which is two of the variables handled by that class.

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved