I need to create subfunctions to do basic Matrix mathematics (addition, subtraction, etc.) I need to be able to pass bot of my Matrices to subfunctions. I started with the addition sub function and I cant get the code to run. I keep getting expected primary-expression before ']' token error for line 75.
#include <iostream>
#include <stdio.h>
using namespace std;
i want to make a two-dimensional array that is consisted by objects but i dont want to initialize the objects when i make the array.. i just want to make an array that each of his "cells" has the size of an object.For example,
class Human{ int hm=5; ... };
and now i want to make a two dimensional array that each "cell" has the size of an Human but not a Human itself.
I am working on the game of fifteen.Yes this is home work. I was wondering if it is possible to save the indexes of an element in a two dim array. I need to find the position of the space in the two dim array save it, find the position of the tile which is passed in as an int and swap the two if they are adjacent. Here is my code
Edit: I can do this by saving i and j in separate int variables but is there a more elegant way of doing this
int board[MAX][MAX]; void init(); void swap(int* lhs, int* rhs);
I have this multi-dimensional array. There are 12 rows, each represents the months, and 2 columns, the 1st column represents the lowest temperature and the 2nd column represents the highest temperature.
I need to find the lowest temperature in only the first column and the highest in only the 2nd column.
This takes in the temperatures. LOHI = 2, MONTHS = 12, LOW = 0, HIGH = 1
This is a test program that takes a number of arguments from the command prompt and concatenates them into a string object. I was looking into the possibility of using the range-based for loop for this purpose. Can it be done with pointer based arrays? I am mainly doing this because I want to have a firm understanding of range-based for, but also would like to do this with least amount of code possible.
This is my working program:
#include <string> #include <iostream> int main(int argc, char *argv[]) { if (argc > 1) { std::string concatenatedArgs;
[Code] ....
Can I somehow replace my while-loop with a range-based for? I tried the following but the compiler points out that begin and end were not declared in the scope of the range-based for loop.
#include <string> #include <iostream> int main(int argc, char *argv[]) { if (argc > 1) { std::string concatenatedArgs;
How can I concatenate two 2-dimensional int arrays into one larger 3-dimensional array. This question is also valid for the 3-dimensional vectors. I know the command for the one dimensional vector as:
I'm writing a program in which I have to use a matrix to represent a file in code. because it's a file, the size of the matrix is undefined, and therefore the matrix has to be dynamic. I found out that my compiler doesn't like dynamic multidimensional arrays, so I was thinking of this matrix as a dynamic (monodimensional) array of other dynamic (monodimensional) arrays. My program (and thus this example) uses unsigned chars.
This is the question; Write a function that builds a two-dimensional multiplication table with arbitrary sizes for the two dimensions.
This is what I have done. I have allowed the user to input whatever size table they want by arbitrarily choosing what value they can input. However I cannot get the board to have blank squares. I thought the char would do it.
Code: #include <iostream> using namespace std; char SQAURE_CHAR = {' '}; const int Board_Size = 14;
I'm now working on a class to handle matrices and matrix operations. I dynamically allocate a two dimensional array with its size as an input. My problem is that i need to resize the matrix structure (to fit to the size of a product of two matrices or to adjust the size after deleting a row or column for example). I make the resizing by deleting the allocated 2d array after saving its first element address to another pointer to pointer, after saving the useful data in an other array, then i use new[] operator to create a new 2d array using the same address of the old deleted array.
void My_matrix ::delete_row(int a) { int i,j; int buffer_row_index = -1; //note that our work is zero based, as we acess the private member of an object of the same class we are working on and we access to the array as it is (zero based) so if this variable is -1, it will be 0 when it enters for the first time and saves the data in the right position. My_matrix buffer(number_of_rows-1,number_of_columns);
[Code] ....
I do this in a way that i think is unsafe. This code is sometimes unstable and gives an unhandled exception whose reason is unknown. I need to know whether this way is good enough or if there any other better ways.
This seems simple enough but I'm missing something here to make this code work.What I'm trying to do is print the contents of the two dimensional array in 25 rows and 4 columns. I experimented with something similar to this code when I initialized the array with numbers.
I'm getting garbage. I tried including the name of the array in the cin object but I got an error message that says "no match for operator '>>'. When I take the name of the array out, the program compiles but I get garbage. Program is suppose to reads data from a file of 25 students and calculates test scores and class grade average output each student's name score, letter grade and grade average. I would be happy at this point if I could just print out something that wasn't garbage.
Here is the code.
#include <iostream> #include <iomanip> #include <cmath> #include <string> #include <fstream> using namespace std; //const const int Array_Row = 25; const int Array_Col = 4;
for (int i=0;i<number;i++){ printf("enter %d th string::",i+1); scanf("%s",str[i]); }
Above, I have a little snippet of a code that I'm trying to figure out. I don't really understand how to implement 2d arrays in C that well. But, I mostly want to know is how and where the strings are being stored, especially with the code below.
Code: for (int i=0;i<number;i++){ printf("enter %d th string::",i+1); scanf("%s",str[i]); }
I know that it's asking the user to enter strings, which will be stored into the 2d array. I just don't understand how it's being stored. Will it be placed in the 1st column or 2nd row or something?
I have a problem with a pointer and a bidimensional array. Here's the code:
typedef char t_board[5][4]; int i,j; t_board *copy (t_board b) { t_board *ptr;
ptr = (t_board *) malloc (sizeof(t_board));
[Code] ...
After this, I print new_board, and its ok; but the following code after the call to copy, crashes. Don't know why. if I remove the statement new_board = copy(some_board); , the code that follows runs ok.
So I started taking a C++ class two weeks ago and we just got to arrays. I am confused on how to pull data from two dimensional arrays. The assignment for this topic reads as follows. Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and the highest and lowest temperatures for the year. Your program must consist of the following functions:
a. Function getData: This function reads and stores data in the two dimensional array. b. Function averageHigh: This function calculates and returns the average high temperature for the year. c. Function averageLow: This function calculates and returns the average low temperature for the year. d. Function indexHighTemp: this function returns the index of the highest high temperature in the array. e. Function indexLowTemp: this function returns the index of the lowest low temperature in the array.
I made the getData function. I do know that i am supposed to more then likely use nested loops on each function to iterate through each column. I have been trying to figure this out. I first saw in my book that the array could be variables so naturally I tryed to make the array loop and increment up the column and row each time a item is stored to make it easier to loop in different functions but then i found out that they have to have a constant value and that doesn't work. Here is the code for index high temp that i have been working on. I honestly feel like i'm over thinking this and its easier then what i'm making it out to be. Here is all of the code.
I am having a little trouble finding the minimum value in my two dimensional array. I feel like my code is right, but it could be returning the wrong value. Below is my code, this is just my "minimum" function!
Code: int minimum( int array[][MAX], int size ) { int i,j,min; for( i = 0; i < size; i++ ) { for( j = 0; j < size; i++ ) { if( array[i][j] < min ) { min = array[i][j];
I am using 2 ARRAYS OF DIFFERENT SIZES in One 2-Dimensional Vector, and my output is not correct. The arrays are size 4 and size 13.
I want COLUMN 0 to have: 55, 66, 77, 88.
I want COLUMNs 1-12 to have 1,2,3,4,5,6,7,8,9,10,10,10,11 in EACH ROW. It would seem that the 2nd loop for the size 13 array would need to loop 4 times in order to fill 4 rows, however, I'm not sure how to do that. Here is what I have so far in code and output:
#include <iostream> #include <vector> using namespace std; int main() { int typeArray[4] = {55,66,77,88}; int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11};
I am trying to pass a dynamic array to a function which will:
- Copy the contents of the array to a temporary dynamic array - Change the array passed in to one size bigger - Copy the elements from the temp array back into the newly changed array - Insert an item into the last spot of the array
Here is my code:
#include <iostream> using namespace std ;
void make_array ( int Old [] , int & old_size , int toInsert ) ; void zero_array ( int arry [] , int arry_size ) ; void print_array ( int arry [] , int arry_size ) ;
[Code] .....
The output seems like a memory address but is just a very large number, what have I done incorrectly to cause this?
I've got a VERY experimental function which takes data stored to a file and assigns it to a multidimensional array on the heap. It's designed for infinite dimensions by recalling itself with updated information but I don't think this is very safe.
The template function creates a heap array using a TYPE**, and recalls itself to create the new dimensions. I want to replace this with the much safer method of assigning just a single heap memory array and then only assign using the recalling method (unless I can find anything else).
To do this though I need to know how single dimensional arrays are stored on the heap, as well as multi-dimensional (for n dimensions). Where I can find this information?
btw I only need this for the Windows operating system, 32bit, I'm not exactly sure what 'C++ style' this is but I'm using Microsoft's Visual Studio Express 2012 as my IDE, so whatever that uses.
Two Hard-coded Arrays into One 2-Dimensional Vector I have 2 arrays, each of them are hard-coded with integer values. I also have one 2-Dimensional vector and I want to put 1 array into the first column of the vector and the other array into the 2nd column of the vector. The reason is that I want to do math on the 2nd column of the vector only.
I am able to accomplish this with 3 arrays. Two of them are 1-Dimensional and the third array is 2-Dimensional.
I receive unexpected outcome in my program. Instead of getting all combinations of 0, 1, 2 (i.e. 000, 001, ..., 222) I get only 000 001 002 010 011 012. The idea of the progarm is to create a crystal lattice. Each atom of the lattice has 3 coordinates (x, y, z). That's why I create class Atom. Then I create 3-dim array of the type derived from class Atom. Now each element of the class will represent an atom.
Code: #include <iostream> using namespace std; class Atom { public: float x, y, z;
I am trying to make quicksort and binary search and I get error when I am passing dynamic array to argument. It also says error during initialization of the dynamic array.
For the past couple of weeks I have been working on a template to hold two-dimensional arrays. Right now I am puzzling over an indexing question.
There are many places in the template where I would like to use initializer_lists to refer to user-specified row and column ranges, particularly in member function arguments. A typical example would be a member function whose declaration would be along the lines of:
Code: Array<Type>::some_function(std::initializer_list<int> columns, std::initializer_list<int> rows); which could get called via
Code: arrayInstance.some_function({3:4}, {5:8});
It would be really nice to be able to use Matlab-style indexing to specify the last column, or the last row, in the Array object -- along the lines of
Code: arrayInstance.some_function({3:4}, {5:END}); where END takes the value -1, and can be defined in Array, or somewhere else.
The way I have tackled this so far was to write myself an Indices PODS class with two elements to hold start and finish values, and a constructor-from-initializer_list that looks something like this:
Code: Indices::Indices(std::initializer_list<int> range, int replace_value) { int const *it = range.begin();
So the elements of "range" give the values of Indices::start and Indices::finish -- but if either of them are entered as END by the user, they will be replaced by replace_value. (The default value of replace_value is END, so Indices::start and Indices::finish will never change if it is omitted.)
I also defined an Indices::endset(int) function to do the same thing for existing Indices objects:
Code: Indices::endset(int replace_value) { if (start == END) start = replace_value; if (finish == END) finish = replace_value; } Using Indices::endset, you can code up Array::some_function by modifying the above signature to something like
Code: Array<Type>::some_function(Indices columns, Indices rows) { columns.endset(this->M); rows.endset(this->N); ... }
This does work, and I've been able to use it in practice. However, it is klutzy. What I would really like to be able to do is have the Indices constructor handle value-replacements in "columns" and "rows", instead of needing to put calls to Indices::endset in every single Array<Type> member function that uses this approach.
The basic problem is that, when Array<Type>::some_function is called, there is no easy way of inserting Array<Type>::M and Array<Type>::N into the optional argument of the Indices constructor when "columns" and "rows" are being built.
The Indices class needs somehow to get access to these, and know which one is being used, M or N. So it needs to have some sort of deeper connection to Array<Type>, but I don't know what that connection should be.
I am working on a number of utility functions for two dimensional arrays of integers, or matrices. However I am having a problem with segmentation faults, most likely due to errors in using malloc in functions like copyMatrix.
Code: matrix_utils.h~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //This function checks if two matrices are equal int isEqual(int **A, int **B, int n);
//This function returns one row of a matrix int * getRow(int **A, int i, int n);
//This function returns one column of a matrix int * getCol(int **A, int j, int n);
I have a firmware application that works well where I display simple text messages on an LCD screen. At the moment, the User Interface is only in English and I have the text strings simply declared as follows:
I want to add a variable that can set the language dynamically from an external device, and so I thought I could do the following:
Code: #define ENG 0 #define FRE 1 #define GER 2 //... static char msg1[ENG][] = " Welcome "; static char msg2[ENG][] = " Data Read ";
[Code] .....
writeLCD(msg1[cLang][]); When I try to compile the above I get a compiler error at the "static char msg..." declarations. The error is: "error: array type has incomplete element type"
Is this method valid, but I have a simple syntax problem with the arrray declarations? Or is this method unworkable and I should find a different method?