Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2d array codes #273

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
int m;
cin >> n;
cin >> m;

vector<vector<int>>marr;
for (int i = 0; i < n; i++) {
vector<int>sarr;
for (int j = 0; j < m; j++) {
int ele;
cin >> ele;
sarr.push_back(ele);
}
marr.push_back(sarr);
}

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << marr[i][j] << " ";
}
cout�<<�endl;
��}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include<iostream>
#include<vector>

using namespace std;

void columnTraversal(vector<vector<int>> mat) {
int row = mat.size();
int col = mat[0].size();

for (int c = 0; c < col; c++) {
if (c % 2 == 0) {
for (int r = 0; r < row; r++) {
cout << mat[r][c];
cout << endl;
}
}
else {
for (int r = row - 1; r >= 0; r--) {
cout << mat[r][c];
cout << endl;
}
}
}
}

int main() {
int n;
int m;
cin >> n;
cin >> m;

vector<vector<int>> mat;
for (int i = 0; i < n; i++) {
vector<int> arr;
for (int j = 0; j < m; j++) {
int ele;
cin >> ele;
arr.push_back(ele);
}
mat.push_back(arr);
}

columnTraversal(mat);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.io.*;
import java.util.*;

public class Main {

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n1 = Integer.parseInt(br.readLine());
int m1 = Integer.parseInt(br.readLine());
int[][] arr1 = new int[n1][m1];

for (int i = 0; i < n1; i++) {
for (int j = 0; j < m1; j++) {
arr1[i][j] = Integer.parseInt(br.readLine());
}
}

int n2 = Integer.parseInt(br.readLine());
int m2 = Integer.parseInt(br.readLine());
int[][] arr2 = new int[n2][m2];

for (int i = 0; i < n2; i++) {
for (int j = 0; j < m2; j++) {
arr2[i][j] = Integer.parseInt(br.readLine());
}
}

if (m1 != n2) {
System.out.println("Invalid input");
return;
}

int[][] prd = new int[n1][m2];
for (int i = 0; i < n1; i++) {
for (int j = 0; j < m2; j++) {
int sum = 0;

for (int k = 0; k < m1; k++) {
sum += arr1[i][k] * arr2[k][j];
}

prd[i][j] = sum;
}
}

for (int i = 0; i < n1; i++) {
for (int j = 0; j < m2; j++) {
System.out.print(prd[i][j] + " ");
}
System.out.println();
      }

   }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <iostream>
using namespace std;

const int mr = 100, mc = 100;
void inputBound(int (&mat)[mr][mc], int n, int m)
{

for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> mat[i][j];
}
}
}

int main(int argc, char** argv)
{


int mat[mr][mc] = {0};
int n, m;
cin >> n >> m;

inputBound(mat, n, m);
int toe = n * m;
int count = 0;
int minr = 0, minc = 0, maxr = n - 1, maxc = m - 1;

while (count < toe)
{

//left wall
for (int i = minr, j = minc ; i <= maxr && count < toe ; i++) {
cout << mat[i][minc] << endl;
count++;
}
minc++;
//bottom wall
for (int i = maxr, j = minc ; j <= maxc && count < toe ; j++) {
cout << mat[maxr][j] << endl;
count++;
}
maxr--;
//right wall
for (int i = maxr, j = maxc ; i >= minr && count < toe ; i--) {
cout << mat[i][maxc] << endl;
count++;
}
maxc--;
//top wall
for (int i = minr, j = maxc ; j >= minc && count < toe ; j--) {
cout << mat[minr][j] << endl;
count++;
}
minr++;
}
// write your code here

��return�0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
using namespace std;

// Pattern 4
void pattern4(int n)
{

int k = 2 * n - 2;


for (int i = n; i > 0; i--) {

for (int j = 0; j < n - i; j++)
cout << " ";
k = k - 2;

for (int j = 0; j < i; j++) {

cout << "* ";
}


cout << endl;
}
}

int main()
{
int n = 5;


pattern4(n);
����return�0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using namespace std;

int main()
{
int n, c, k, space = 1;

cout << �\nEnter the number of rows : �;
cin >> n;

space = n � 1;

for (k = 1; k <= n; k++)
{
for (c = 1; c <= space; c++)
cout << � �;
space--;
for (c = 1; c <= 2*k-1; c++)
cout << �*�;
cout << endl;
}
space = 1;
for (k = 1; k <= n � 1; k++)
{
for (c = 1; c <= space; c++)
cout << � �;
space++;
for (c = 1 ; c <= 2*(n-k)-1; c++)
cout << �*�;
cout << endl;
}
return�0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
using namespace std;
int main()
{
int i, j, n=3;
for(i = 0; i < n; i++)
{
for(j = 0; j < (2 * n); j++)
{
if(i + j <= n - 1)
cout << "*";
else
cout << " ";
if((i + n) <= j)
cout << "*";
else
cout << " ";
}
cout << "\n";
}

for(i = 1; i < n; i++)
{
for(j = 0; j < (2 * n); j++)
{
if(i >= j)
cout << "*";
else
cout << " ";
if(i >= (2 * n - 1) - j)
cout << "*";
else
cout << " ";
}
cout << "\n";
}
����return�0;
}