C :: Unable To Print Out A Two Dimensional Array
Feb 1, 2013
I have a very simple program here, and somehow I can't get it to print out a two-dimensional array. Here's my code:
Code:
#include <stdio.h>
#define MAX_PEOPLE 100
char display_table(int table[MAX_PEOPLE][MAX_PEOPLE], int n);
int main (void) {
int n = 5;
char names[MAX_PEOPLE][4] = {"Ami", "Bob", "Cal", "Dan", "Ion"};
[Code]...
and here's how the printed table should look like when i run the program:
Code:
|Ami Bob Cal Dan Ion
--- --- --- --- --- ---
Ami| 0 1 1 1 0
Bob| 0 0 1 0 0
Cal| 0 0 0 0 0
Dan| 1 0 1 0 1
Ion| 0 1 1 1 0
View 3 Replies
ADVERTISEMENT
Apr 27, 2014
I am new to C++ and I am having an issue reading in a 2 Dimensional array from a data file. I am very close to reading it in perfectly except for one issue, the loop is ignoring the first value from the data file.
This is the code I have so far:
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <functional>
#include <conio.h>
#include <iomanip>
#include <string>
#include <fstream>
[Code]...
This is the values from the data file:
300 450 500 210
510 600 750 400
627 100 420 430
530 621 730 530
200 050 058 200
100 082 920 290
When the Program shows my array it comes up as:
450 500 210 510
600 750 400 627
100 420 430 530
621 730 530 200
050 058 200 100
082 920 290 0
Every array location is moved up one, like the program just completely ignored the 300 at the beginning.
View 1 Replies
View Related
Nov 25, 2013
I seem to get an error after int main (void) saying 'a function definition isnt aloowed here before { token? And then also at the end of main saying 'expexted } at end of output?
My programme is trying to create a random array of 100 ints between 0 and 250, sort them and print them.
Here is my code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cstring>
void bubbleSort(int *array,int length)//Bubble sort function {
int i,j;
for(i=0;i<10;i++)
[Code]....
View 4 Replies
View Related
Apr 4, 2015
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.
#include <iostream>
#include <iomanip>
#include <cmath>
[Code].....
View 1 Replies
View Related
Apr 4, 2015
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;
[Code] ......
View 1 Replies
View Related
Jul 31, 2013
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:
std::vector<int> results;
results.reserve(arr1.size() + arr2.size());
results.insert(results.end(), arr1.begin(), arr1.end());
results.insert(results.end(), arr2.begin(), arr2.end());
and for the one dimensional array as:
int * result = new int[size1 + size2];
copy(arr1, arr1 + size1, result);
copy(arr2, arr2 + size2, result + size1);
But I do not know how to make a 3-dimensional array or vector.
View 3 Replies
View Related
May 12, 2014
I am having trouble in reading data from my text file and print it out exactly like how it looks like in the text file. The problem im having is it will not read the last y Coordinates of the point. it keep reading the second last point for y coordinates which is wrong.
my text file is
0.0 0.0
0.0 1.0
1.0 0.0(but it read until here)
0.0 0.0(suppose to read the last point which is here)
For your information, this is my 1st year degree assignment in C programming. It is to create a program which can read text file (manually create) and print it out in a program and calculate the area for the polygon using ADT function ( .c and .h files)
*This is the code for my read file function*
Basically this accepts a Polygon and a file pointer as parameters. It reads the polygon point data from the file, pass the read data to plg_new() to create a new Polygon and returns the new Polygon created.
Code:
polygon *plg_read(polygon *New_polygon, FILE *Coord) {
int i;
int numberofvertices=0;
int count=0;
char filename[50];
double xCoor[50], yCoor[50];
[Code]....
This is the second function my polygon new code. This ADT function basically creates a new Polygon with malloc(), initialize all ADT data members with its parameter values and returns the Polygon.
Code:
polygon *plg_new(double *xCoordinates, double *yCoordinates, int numberOfVertices) {
int x;
polygon *New_poly = (polygon *)malloc(sizeof (polygon));
if(New_poly->xCoordinates == NULL || New_poly->yCoordinates == NULL) {
free(New_poly);
[Code]....
This is the rest of the code if you need to refer to other codings.
Code:
/**
*@file polygon.c
*@brief Functions for polygon / Struct has polygon numberOfVertices, polygon *xCoordinates and polygon *yCoordinates
*@author: Tan Xian Yao
*@id: 4323440
*@date: 22/04/2014
*/
[Code]...
View 6 Replies
View Related
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
Mar 19, 2014
is it allowed to to like this:
char a[10] = "Lizard";
char b[2][5];
b[0][0] = a[0];
b[0][1] = a[1]; etc?
View 1 Replies
View Related
Sep 2, 2013
I have a 3D array that contains 200 strings. I'm trying to copy all these strings into a 2D array. How can this be done? This is what I have so far but it isn't working correctly.
Code:
for(int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
{
dest[i][j] = source[0][i][j];
} }
The finished product would be with 100 rows, 2 columns.
View 4 Replies
View Related
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
Jan 10, 2015
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
View 1 Replies
View Related
Apr 20, 2013
I was able to get the max to work but I cant get the min. it returnning something like -125863456.
Code:
int maxx(int a[][10]);
int minn(int a[][10]);
int main(void){
int array[][10] = { { 2, 7, 6, 8, 4}, {3, 9, 1, 5, 6} };
int max1, min1;
[Code] .....
View 3 Replies
View Related
May 5, 2013
Code:
#include <stdio.h>
char * strcpy(char *restrict s1, const char *restrict s2);
struct item {
char title[20];
struct item *next;
} firstCard;
int main (){
strcpy(firstCard.next->title, "whatever");
}
I am unable to set firstCard.next->title
View 2 Replies
View Related
Oct 25, 2013
I want to make a program that asks the user for a message and then print out a large graphic of that message. For example, if the user types "he" I want to print out
H..................H EEEEEEEEE
H..................H E
H..................H E
H..................H E
HHHHHHHHHH EEEEEEEEE
H..................H E
H..................H E
H..................H E
H..................H EEEEEEEEE
(treat the periods as spaces. I only put them there because it wouldn't separate the H's correctly.)
I will loop this to continue until the user types quit.
1. How would I set this up to store the user input characters into an array?
2. How would I print out the stored data in the shape of the word?
View 4 Replies
View Related
Dec 19, 2014
Is it possible to create an 8 dimensional array in C? If no, then what can I do to improvise or let say bypass the limit?
View 7 Replies
View Related
Oct 21, 2014
Consider the following code snippet:
GLfloat box[4][4] = {
{ x2, -y2, 0, 0 },
{ x2 + w, -y2, 1, 0 },
{ x2, -y2 - h, 0, 1 },
{ x2 + w, -y2 - h, 1, 1 },
};
Do I need to call a variant of
delete [] box;
Against this float array?
View 1 Replies
View Related
Apr 1, 2013
int sum(int (*ar2)[4], int size);
// I dont know what the ar2 is going on
int main(){
int data[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
int total = sum(data, 3)
return 0;
}
View 4 Replies
View Related
Jan 9, 2014
Here is what I have, I have a 1D Array being added to a 2D Array and I need to Sort them by value value 3 in the 2D Array, while maintaining a specific amount. Here is what I have so far:
public static void CheckHS(string[] HS) {
try {
GeneralData.HighScores[10, 0] = "11";
GeneralData.HighScores[10, 1] = HS[1];
GeneralData.HighScores[10, 2] = HS[2];
GeneralData.HighScores[10, 3] = HS[3];
//Need Sort Data - Bubble Sort?
}//end Try
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
I am thinking bubble sorting but I remember reading about something faster. Unfortunately I can't find it on the web. The idea is that there will be always 10 Values and 4 Columns on the 2D Array. [The 11th Row being empty at the end of it.
View 14 Replies
View Related
Apr 24, 2013
I want to create 4 dimensional array, in that first three dimenstional are fixed size and the final index will be on 0 to N-numbers.
E.g., double array[500][25][10][<NOT FIXED>].. So I cant create statically, because the index size are more. Also I have tried 4 dimenstional vector, but its giving 2 problem.
(i) I am storing 4th dimenstion size is more than vector[0][0][0].max_size()
(ii) Storing and Retrieving its more time in vector
So, any other solution to store large array which is 3 index is FIXED and final one is not FIXED?
View 16 Replies
View Related
Feb 26, 2012
i created a program which uses Sparse 2 dimensional array, but i am not sure if i did it in the right way .
this is the instruction i have:
Create a constructor and a destructor. The constructor should take as input the size of the array (consider only square NxN arrays, so only one dimension is needed) and the thickness of the ribbon. To make this precise, if supplied with a thickness parameter t, you may assume that the element [0,t] (i.e. the (t+1)-th element of the first row) is where the useless area begins on the right. Similarly, the element [t,0] is where the useless area begins on the left. The border of the useless areas moves diagonally down and to the right, i.e. it consists of [1,t+1],[2,t+2],... and [t+1,1],[t+2,2],... The above example has thickness 3.
The space for the 2-d array should be dynamically allocated and must be large enough to fit the useful data only.Create methods for random read and write access to the array as in the case of 1-d arrays.Overload the [] and << operators, as in the case of 1-d arrays. Think carefully about what the [] operator should return and how it should work. Ideally we would like this to behave in a manner similar to standard 2-d arrays (i.e. accessing elements in the normal way, like x[5][6]).
View 3 Replies
View Related
Jan 30, 2013
here is my code, I'm trying to read a .txt file into a 2D array... what's wrong with my code?
Code:
#include <stdio.h>
#define INPUT_FILE_GET_NAMES "names.txt"
#define INPUT_FILE_GET_SALES "sales.txt"
#define MAX_ROWS_NAMES 25
#define MAX_COLS_NAMES 20
#define MAX_ROWS_SALES 25
#define MAX_COLS_SALES 6
//Function Declarations
char getNames(char namesArray[][MAX_COLS_NAMES], char filename[]);
[Code]...
View 1 Replies
View Related
Mar 4, 2014
Code:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <math.h>
void reverseAr(char ar[], int n);
int main() {
int choice;
char *abc= ar[];
[Code]...
My codes keep couldn't get the reverse array of characters.
View 4 Replies
View Related
Mar 12, 2014
I'm having some problems with Input to an array from a file. I think that i need to make a new array for every instance of the loop but i can't figure out how.
Participator class
void Participator::read(ifstream &stream){
getline(stream, name);
getline(stream, address);
stream >> paid;
}ParticipatorRegister class
void ParticipatorRregister::readFromFile(){
[Code]...
View 1 Replies
View Related
Jan 9, 2015
This code shows a loop inside a loop initializing a two-dimensional array named arr with elements [2] and [5]. Here's my code:
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
[Code]....
Now, what I wanted to do is to have an output showing the sums of each elements. Example, the above code has an output of:
1 2 3 4 5
6 7 8 9 10
I wanted to have more codes which would add up the elements 1 + 6, 2 + 7, 3 + 8, 4 + 9, 5 + 10 and produce and output:
7 9 11 13 15
View 8 Replies
View Related
Jan 12, 2015
I have a 10x10 array, initialized to all zeros. I create 2 random numbers for the purpose of guessing a position in the array.
However, when I print the array, the 0,0 spot keeps shifting to match the 2nd random number generated. Is there something strange about the 0,0 spot?
Here is my code:
Code:
seedrnd();
for (x=0;x<2;x++) {
randArray[x]=rnd(10);
}
for (x=0;x<2;x=x+1)
[Code] .....
The 2nd to last print statement actually prints randColumn. The last print statement correctly prints 0.
View 7 Replies
View Related