I have created a 2d array, so lets say as I did in the code below the array elements shows like this:
3 4
8 2
I want to compare the" ROWS" and get the minimum number, like what I do with 3 & 4, I get 3 is the min number and with the second row between 8 & 2, I get 2 is the min number.
My problem is that I can't get it to compare the columns as in 3 & 8 to get the max out between them, and 4 & 2.
In the code it just outputs the max number in the rows it still comparing the "ROWS"
P.S sorry for the long explanation. I just had to clarify it .
int main()
{
int d;
int max;
//array dimention
cout << "Enter the array dimention :- " << endl;
cin >> d;
//max_min for the final maximum numbers
int max_num[d];
//the 2d array
int arr[d][d];
// array input
cout << "please enter the array elements :- " << endl;
for (int i = 0; i < d; i++) {
for (int j = 0; j < d; j++) {
cin >> arr[i][j];
}
}
//array output
cout << "the array elements you enterd :-" << endl;
for (int i = 0; i < d; i++) {
for (int j = 0; j < d; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
//the max numbers
for (int i = 0; i < d; i++) {
max = arr[0][i];
for (int j = 0; j < d; j++) {
if (max < arr[j][i]) {
max = arr[j][i];
}
}
max_num[i] = max;
}
cout << "the maximum elements in array :-" << endl;
for (int i = 0; i < d; i++) {
cout << max_num[i] << endl;
}
}
Related
Im trying to create a program that creates two 2d dynamic arrays and multiply them and give an output and calculate the time taken for the multiplication process based on the input size n. This code works when it comes to outputs lesser than 7 rows and 7 cols but gives an error when the number goes above 8.
using namespace std;
int m1c , m1r , m2c , m2r , i , j , k , l;
int** arr1 = new int*[m1c];
int** arr2 = new int*[m2c];
int** multArr = new int*[m1c];
int main(){
cout << "Enter the Number of rows for matrix 1 :";
cin >> m1c;
cout << "Enter the Number of columns for matrix 1 :";
cin >> m1r;
cout << "Enter the Number of rows for matrix 2 :";
cin >> m2c;
cout << "Enter the Number of columns for matrix 2 :";
cin >> m2r;
for (i = 0; i < m1r; i++) {
arr1[i] = new int[m1c];
multArr[i] = new int[m1c];
}
for (i = 0; i < m2r; i++) {
arr2[i] = new int[m2c];
}
if (m1r != m2c) {
cout << "Number of rows in the first matrix must be equal to the numbr of columns in the second matrix ";
return -1;
}
for (i = 0; i < m1r; i++) {
for (j = 0; j < m2c; j++) {
arr1[i][j] = rand() % 100;
}
}
for (i = 0; i < m2r; i++) {
for (j = 0; j < m2c; j++) {
arr2[i][j] = rand() % 100;
}
}
//Displaying the two arrays
for (i = 0; i < m1r; i++) {
for (j = 0; j < m1c; j++) {
cout << arr1[i][j] << " ";
}
cout << endl;
}
cout << endl;
for (i = 0; i < m2r; i++) {
for (j = 0; j < m2c; j++) {
cout << arr2[i][j] << " ";
}
cout << endl;
}
delete[] arr1;
delete[] arr2;
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Looks like an error initializing arr1. Your using m2c as the column count. You probably meant m1c.
Lets suppose we have a 5 X 5 random array
1 2 3 7 8
4 7 3 6 5
2 9 8 4 2
2 9 5 4 7
3 7 1 9 8
Now I want to print the right side of the diagonal shown above, along with the elements in the diagonal, like
----------8
--------6 5
------8 4 2
---9 5 4 7
3 7 1 9 8
The code I've written is
#include <iostream>
#include <time.h>
using namespace std;
int main(){
int rows, columns;
cout << "Enter rows: ";
cin >> rows;
cout << "Enter colums: ";
cin >> columns;
int **array = new int *[rows]; // generating a random array
for(int i = 0; i < rows; i++)
array[i] = new int[columns];
srand((unsigned int)time(NULL)); // random values to array
for(int i = 0; i < rows; i++){ // loop for generating a random array
for(int j = 0; j < columns; j++){
array[i][j] = rand() % 10; // range of randoms
cout << array[i][j] << " ";
}
cout << "\n";
}
cout << "For finding Max: " << endl;
for(int i = 0; i < rows; i++){//loop for the elements on the left of
for(int j = columns; j > i; j--){//diagonal including the diagonal
cout << array[i][j] << " ";
}
cout << "\n";
}
cout << "For finding Min: " << endl;
for(int i = rows; i >= 0; i++){ //loop for the lower side of
for(int j = 0; j < i - columns; j++){ //the diagonal
cout << array[i][j] << " ";
}
cout << "\n";
}
return 0;
}
After running the code the shape I get is correct , but the elements do not correspond to the main array. I have no idea what the problem is.
Left side:
for (size_t i = 0; i < rows; i++) {
for(size_t j = 0; j < columns - i; j++) {
cout << array[i][j] << " ";
}
cout << "\n";
}
Right side:
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < columns; j++) {
if (j < columns - i - 1) cout << "- ";
else cout << vec[i][j] << " ";
}
cout << "\n";
}
I need to write a program that receives 2 arrays and checks how many times 1 is included in the other...
But I cant find what is wrong with my program! tx!!
#include <iostream>
using namespace std;
int main()
{
int vector1[500];
int vector2[100];
int a = 0, b = 0, count = 0, k = 0;
cout << "enter size of first array:" << endl;
cin >> a;
cout << " enter first array values:" << endl;
for (int i = 0; i < a; i++)
cin >> vector1[i];
cout << "enter size of second array:" << endl;
cin >> b;
cout << "enter secound array values:" << endl;
for (int i = 0; i < b; i++)
cin >> vector2[i];
for (int i = 0; i < b; i++)
for (int j = 0; j < a; j++)
if (vector2[i + k] == vector1[j])
{
count++;
k++;
}
else
k = 0;
cout << count << endl;
system("pause");
return 0;
}
Why at all do you need k? The problem is about all inclusions of all elements right? If O(n^2) complexity is fine, then...
for (int i = 0; i < b; i++)
for (int j = 0; j < a; j++)
if (vector2[i] == vector1[j])
count++;
One obvious disadvantage of the code above is that you'll get the total sum of all occurences of elements from vector1 in vector2. The key idea remains the same in case you need to know, which elements exactly appeared in another array and how many times, you'll just have to use map or other vector.
I am trying to write a program to count each number the program has encountered. by putting M as an input for the number of the array elements and Max is for the maximum amount of number like you shouldn't exceed this number when writing an input in the M[i]. for some reason the program works just fine when I enter a small input like
Data input:
10 3
1 2 3 2 3 1 1 1 1 3
Answer:
5 2 3
But when I put a big input like 364 for array elements and 15 for example for max. the output doesn't work as expected and I can't find a reason for that!
#include "stdafx.h"
#include <iostream>
#include<fstream>
#include<string>
#include <stdio.h>
#include<conio.h>
using namespace std;
int ArrayValue;
int Max;
int M[1000];
int checker[1000];
int element_cntr = 0;
int cntr = 0;
int n = 0;
void main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> ArrayValue>> Max;
for (int i = 0; i < ArrayValue; i++)
{
cin >> M[i];
checker[i]= M[i] ;
element_cntr++;
if (M[i] > Max)
{
cout << "the element number " << element_cntr << " is bigger than " << Max << endl;
}
}
for (int i = 0; i < Max; i++)
{
cntr = 0;
for (int j = 0; j < ArrayValue; j++)
{
if (M[n] == checker[j])
{
cntr+=1;
}
}
if (cntr != 0)
{
cout << cntr << " ";
}
n++;
}
}
You have general algorithm problem and several code issues which make code hardly maintainable, non-readable and confusing. That's why you don't understand why it is not working.
Let's review it step by step.
The actual reason of incorrect output is that you only iterate through the first Max items of array when you need to iterate through the first Max integers. For example, let we have the input:
7 3
1 1 1 1 1 2 3
While the correct answer is: 5 1 1, your program will output 5 5 5, because in output loop it will iterate through the first three items and make output for them:
for (int i = 0; i < Max; i++)
for (int j = 0; j < ArrayValue; j++)
if (M[n] == checker[j]) // M[0] is 1, M[1] is 1 and M[2] is 1
It will output answers for first three items of initial array. In your example, it worked fine because the first three items were 1 2 3.
In order to make it work, you need to change your condition to
if (n == checker[j]) // oh, why do you need variable "n"? you have an "i" loop!
{
cntr += 1;
}
It will work, but both your code and algorithm are absolutely incorrect...
Not that proper solution
You have an unnecessary variable element_cntr - loop variable i will provide the same values. You are duplicating it's value.
Also, in your output loop you create a variable n while you have a loop variable i which does the same. You can safely remove variable n and replace if (M[n] == checker[j]) to if (M[i] == checker[j]).
Moreover, your checker array is a full copy if variable M. Why do you like to duplicate all the values? :)
Your code should look, at least, like this:
using namespace std;
int ArrayValue;
int Max;
int M[1000];
int cntr = 0;
int main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> ArrayValue >> Max;
for (int i = 0; i < ArrayValue; i++)
{
cin >> M[i];
if (M[i] > Max)
{
cout << "the element number " << i << " is bigger than " << Max << endl;
}
}
for (int i = 0; i < Max; i++)
{
cntr = 0;
for (int j = 0; j < ArrayValue; j++)
{
if (i == M[j])
{
cntr ++;
}
}
if (cntr != 0)
{
cout << cntr << " ";
}
}
return 0;
}
Proper solution
Why do you need a nested loop at all? You take O(n*m) operations to count the occurences of items. It can be easily counted with O(n) operations.
Just count them while reading:
using namespace std;
int arraySize;
int maxValue;
int counts[1000];
int main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> arraySize >> maxValue;
int lastReadValue;
for (int i = 0; i < arraySize; i++)
{
cin >> lastReadValue;
if (lastReadValue > maxValue)
cout << "Number " << i << " is bigger than maxValue! Skipping it..." << endl;
else
counts[lastReadValue]++; // read and increase the occurence count
}
for (int i = 0; i <= maxValue; i++)
{
if (counts[i] > 0)
cout << i << " occurences: " << counts[i] << endl; // output existent numbers
}
return 0;
}
Im supposed to display grades in a graph that shows how many grades are in each interval 0-10, 10-20, 20-30 etc. By putting a * for each grade in the interval. My only problem is that when the array grade[] has 1, 2, 3 or 5 slots it puts an extra * in the 0-10 interval. It works with every other ammount of slots.
void sort_grades(int grades[],int students)
cout << endl << endl;
cout << " THE GRADES GRAPH"<< endl;
cout << "=============================="<<endl;
const int max_grade = 100;
const int interval=10;
for (int j = 0; j < max_grade; j+=10)
{
cout << j << " - " << (j+interval) << " : ";
for (int k = 0; k <= students; k++)
{
if (j==90 && grades[k] ==(j+interval) )
{
cout<< "*";
}
else if (grades[k] < (j+interval) && grades[k] >= j)
{
cout<< "*";
}
}
cout << endl;
}
I assume that students is the size of the array grades[].
In this case, you access too many elements in the following loop:
for (int k = 0; k <= students; k++)
Instead the line should read:
for (int k = 0; k < students; k++)
This problem does not only occur for the cases where grade[] has a size of 1, 2, 3 or 5. You only see the problem for that sizes, because the output depends on the content of the array.