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
ADVERTISEMENT
Aug 20, 2014
I want to convert the matrix to vector and vice verse.
View 1 Replies
View Related
Mar 27, 2013
i want to know how i can solve this question? do i need to create a class or write the program codes.
View 12 Replies
View Related
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
View Related
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
Sep 24, 2013
1. Input an dimension and elements of an array from the keyboard. Count the odd elements of an array and that number join to variable K, then sort the last K elements in decreasing order.
Code:
#include <stdio.h>
main ()
{
int A[100], i, n, j, K = 0;
printf ("Type in the dimension of an array:
");
scanf ("%d", &n);
[Code]....
View 7 Replies
View Related
Jun 1, 2014
I just want to know the code of the program: Write code to accept matrix as aurgument and display its multiplication matrix which return its multiplication matrix.
View 1 Replies
View Related
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
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
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
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
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
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
Jun 3, 2013
I'm trying to copy my array 'block' to a 'dummy' 3D matrix so I can take out some arbitrary smaller matrix. Shouldn't this be possible with std::copy, where I'm certain the number of elements in the 1D array are equivalent to those in the dummy?
int dummy[210][210][1000];
std::copy(&block[0], &block[block.size()], &dummy);
View 2 Replies
View Related
Jan 3, 2013
Array Manipulation - Transpose of a Square Matrix: This program reads a matrix (two dimensional array), displays its contents, transposes it and then displays the transposed matrix. And here's the code
Code:
#include <iostream.h>
const int arraySize = 3;
void readMatrix(int arr[][arraySize]);
void displayMatrix(int a[][arraySize]);
void transposeMatrix(int a[][arraySize]);
[Code] ....
When I compile the program it says 'mail' must return 'int' I think everything is right...
View 2 Replies
View Related
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
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
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
Feb 27, 2012
I want to assign a matrix to submatrix of a bigger matrix.
ublas::matrix<int> A(8,5);
ublas::matrix<int> B(2,5);
for(size_t i=0;i<A.size1();++i)
for(size_t j=0;j<A.size2();++j)
A(i,j)=i*A.size2()+j+1;
for(size_t i=0;i<B.size1();++i)
for(size_t j=0;j<B.size2();++j)
B(i,j)=i*B.size2()+j+5;
ublas::matrix_range<ublas::matrix<int> > a(A,ublas::range(0,2),ublas::range(0,5));
a=B;
and it works.
but if the matrix is compressed_matrix type, there's something with it. the error log as below:
Check failed in file boost_1_48_0/boost/numeric/ublas/detail/matrix_assign.hpp at line 1078:
detail::expression_type_check (m, cm)
terminate called after throwing an instance of 'boost::numeric::ublas::external_logic'
what(): external logic
Aborted
I'm not sure this is a bug or not.
View 2 Replies
View Related
Nov 9, 2014
Essentially what I need to do is take a text file, ("input.txt"):
4 4
1 0 0 1
1 1 1 1
0 0 1 0
0 0 1 0
And take the first two values on line 1 (4, 4) and use them as length and width.
Number of rows: 4
Number of columns: 4
Then I need to print out the matrix and further manipulate it. I need to find the sum of the 1's per column and then take that number and replace each 1 with the 1's in each column.
So it'll look like this:
2 0 0 2
2 1 3 2
0 0 3 0
0 0 3 0
The part that's mostly troubling me is that my instructor will be giving me the input file with random values, so I don't know what the matrix dimensions will be.
I can read the 2D array but can't seem to use it after. I need to find a way to skip the first line, and then read in the matrix and be able to use it mathematically to add up each column.
View 1 Replies
View Related
Jan 8, 2015
Using two-dimensional arrays, write a program which multiplies an mxn matrix of integers by an nxr
matrix of integers.
INPUT FIRST (2x2) MATRIX:
Type in 2 values for row 1 separated by spaces: 3 4
Type in 2 values for row 2 separated by spaces: 5 7
INPUT SECOND (2x2) MATRIX:
Type in 2 values for row 1 separated by spaces: 1 1
Type in 2 values for row 2 separated by spaces: 2 2
3 4
5 7
TIMES
1 1
2 2
EQUALS
11 11
19 19
View 1 Replies
View Related
Jan 31, 2014
what if we want to print both the diagonal elements!!!!
View 1 Replies
View Related
Oct 8, 2014
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int a,b;
int Rowsize , Colsize ;
int A[Rowsize][Colsize];
[Code] ....
View 1 Replies
View Related
Jul 27, 2013
I have an array matrix called tmat,,and i know that in every row of tmax there are values which repeat two times...and am writing a code to extract the values WHICH DOES NOT REPEAT into another matrix called tcopy...the codes compiles fine...and it writes nicely to file...but without the desired result...
One last question...how can i get the array tcopy written to file in the form 5x3...and not all the figures in line one after the other? i mean i wish to see the matrix like a matrix on file..not like a list of numbers....
Code:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const int R = 5;
const int C = 5;
[Code] ....
View 14 Replies
View Related
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
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