I want to shift value in multi dimensional array, i try but its not working at all and am also confused within logic.
here what i want to do
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
and let i pass 1, 2 so it should delete 5 from array and shift all elements.
Expected Output:
1, 2, 3,
4, 6, 7,
8, 9, 0
Here is my code.
#include <iostream>
void display(int arr[][3], int size)
{
std::cout << "\nDisplay of array:\n";
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
std::cout << i << " " << j << " : " << arr[i][j] << std::endl;
}
}
return;
}
void insert(int arr[][3], int size)
{
std::cout << "\nInserting element in an array:\n";
int pos, num;
std::cout << "Enter new value to be inserted: ";
std::cin >> num;
std::cout << "Enter the position where you wanted to inserted it: ";
std::cin >> pos;
/*for (int i = size - 2; i >= pos; i--) {
arr[i+1] = arr[i];
}*/
//arr[pos] = num;
display(arr, size);
}
void delete_ele(int arr[][3], int size)
{
std::cout << "\nDeleting an array:\n";
int posX, posY;
display(arr, size);
std::cout << std::endl;
std::cout << "Enter the X pos where you wanted to delete element: ";
std::cin >> posX;
std::cout << "Enter the Y pos where you wanted to delete element: ";
std::cin >> posY;
const int s = 3;
int newArr[s][s];
int x = 0, y = 0;
bool t = false;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == posX && j == posY) {
t = true;
continue;
}
newArr[x][y] = arr[i][j];
y++;
}
if (t) {
continue;
}
x++;
}
display(newArr, size);
}
int main(void)
{
// size of array.
const int size = 3;
//Decleration of arrays
int arr[size][size] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int arr2[size][size] = {
{1, 2, 3},
{7, 8, 9}
};
// Delete an array basse on position.
delete_ele(arr, size);
// Insert an array by shifting..
insert(arr2, size);
return 0;
} // end of main.
Thanks
Related
i am trying to get the total amounts of numbers occurunces , my main problem is that i messed up some where and i cant read numbers that are higher than 12 , as in my count wont see it other than that works perfectly , doesnt matter if sorted on not array doesnt affect the program(for my random array example)
*** int Count(int r[], int n, int x) {
int res = 0;
for (int i = 0; i <n; i++)
if (x == r[i])
res++;
return res;
}
int main() {
int count = 0;
int r[12] = { 1, 1, 2, 3, 4, 5, 6, 6, 7, 8,13,13 };
int n = sizeof(r) / sizeof(r[0]);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++) {
if (r[i] > r[j])
{
swap(r[i], r[j]);
}
}
}
for (int i = 0; i <= n; i++) {
if (Count(r, n, i) >= 2) {
count++;
cout << "number" << i << "-" << Count(r, n, i) << " Recurrences" << endl;
}
}
cout << "count is " << count;
return 0;
} ***
Try an easier and more optimized way using map:
int r[12] = { 1, 1, 2, 3, 4, 5, 6, 6, 7, 8, 13, 13 };
int n = sizeof(r) / sizeof(r[0]);
map<int, int> map;
for (int i = 0; i < n; i++)
{
if (map.find(r[i]) != map.end())
{
map[r[i]]++;
}
else
{
map[r[i]] = 1;
}
}
for (auto const& x: map)
{
cout << x.first << ':' << x.second << std::endl;
}
output
1:2
2:1
3:1
4:1
5:1
6:2
7:1
8:1
13:2
I have a code that reorders arrays based on an index. The code works fine when hardwired to add the numbers in through the editor, but how do I switch that to prompt the user to have to enter the numbers for both the array and index through terminal?
#include <iostream>
using namespace std;
void reorder(int arr[], int index[], int n)
{
int temp[n];
for (int i = 0; i < n; i++)
temp[index[i]] = arr[i];
for (int i = 0; i < n; i++) {
arr[i] = temp[i];
index[i] = i;
}
}
int main()
{
int arr[] = { 3, 2, 1, 5, 6, 4 };
int arb[] = { 5, 6, 1, 3, 2, 4 };
int index[] = { 3, 4, 2, 0, 1, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "\nSequence array is: \n";
for (int i = 0; i < n; i++)
std::cout << index[i] << ' ';
cout << "\nOriginal array is: \n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
printf("\n");
reorder(arr, index, n);
reorder(arb, index, n);
cout << "Reordered array is: \n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
printf("\n");
printf("\n");
return 0;
}
Have a look on your reorder function. As PaulMcKenzie commented, the code works (if corrected maybe like this):
void reorder(int arr[], int index[], int n)
{
vector<int> temp(n);
for (int i = 0; i < n; i++)
temp[index[i]] = arr[i];
for (int i = 0; i < n; i++) {
arr[i] = temp[i];
index[i] = i;
}
}
see live demo.
Explanation: arrays with variable length are not part of any of the C++ standards. You can use a vector for your purpose that is initialized with n default int values (i.e. 0). This is necessary for randomly accessing it. Of course, your solution is somewhat weak/limited because you have to ensure that the values in index are in range: (0 <= v) && (v < NELEMS(arr)).
As Tomas above mentions, use std::vector instead. However, if you really want to use int[] the following should work.
int nDigits = 0;
cout << "Enter number of digits:" << endl;
cin >> nDigits;
int* arr;
int* index;
arr = new int[nDigits];
index = new int[nDigits];
for(int i = 0; i < nDigits; i++){
cout << "Enter arr element (" << i << "):" << endl;
cin >> arr[i];
cout << "Enter index element (" << i << "):" << endl;
cin >> index[i];
}
... Use vectors instead tbh
You have the problem of variable-length arrays. The simplest but really bad solution is to use a global compile-time constant for the array size. You can use std::cin to read the values from terminal. By the way you shouldn't mix std::cout and std::printf. Use std::cout.
You should add a check of the user input if each entry is unique and in the range [0, n-1].
#include <iostream>
using namespace std;
constexpr unsigned int n = 6;
void reorder(int arr[], int index[])
{
int temp[n];
for (int i = 0; i < n; i++)
temp[index[i]] = arr[i];
for (int i = 0; i < n; i++) {
arr[i] = temp[i];
index[i] = i;
}
}
int main()
{
int arr[] = { 3, 2, 1, 5, 6, 4 };
int arb[] = { 5, 6, 1, 3, 2, 4 };
int index[n];
std::cout << "Enter " << n << " values: ";
for (auto &element : index) {
std::cin >> element;
}
cout << "\nSequence array is: \n";
for (int i = 0; i < n; i++)
std::cout << index[i] << ' ';
cout << "\nOriginal array is: \n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
printf("\n");
reorder(arr, index);
reorder(arb, index);
cout << "Reordered array is: \n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
printf("\n");
printf("\n");
return 0;
}
The best solution is to use std::vector
#include <iostream>
using namespace std;
void reorder(std::vector<int> arr, std::vector<int> index)
{
auto n = arr.size();
std::vector<int> temp(n);
for (int i = 0; i < n; i++)
temp[index[i]] = arr[i];
for (int i = 0; i < n; i++) {
arr[i] = temp[i];
index[i] = i;
}
}
int main()
{
std::vector<int> arr = { 3, 2, 1, 5, 6, 4 };
std::vector<int> arb = { 5, 6, 1, 3, 2, 4 };
auto n = arr.size();
std::vector<int> index(n);
std::cout << "Enter " << n << " values: ";
for (auto &element : index) {
std::cin >> element;
}
cout << "\nSequence array is: \n";
for (int i = 0; i < n; i++)
std::cout << index[i] << ' ';
cout << "\nOriginal array is: \n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
printf("\n");
reorder(arr, index);
reorder(arb, index);
cout << "Reordered array is: \n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
printf("\n");
printf("\n");
return 0;
}
To get an input from the command line use cin. You'd also need to use a vector instead of an array to hold a "dynamic array", i.e. an array you don't know the size of at compile time.
Best practice is to put that input into a string, and parse that string, as cin does not handle incorrect input (include string for this).
string input;
cin >> input;
next you need to split the input - it is easiest if the delimiter is space, for a comma, you'd need a different method (you'd need to include iterator, vector and sstream for this)
istringstream iss(input);
vector<string> splitted_input{istream_iterator<string>(iss), istream_iterator<string>()};
and convert it to numbers (include algorithm for this)
vector<int> parsed_input(splitted_input.size());
transform(splitted_input.begin(), splitted_input.end(), parsed_input.begin(), [](auto val){ return stoi(val);});
alternatively, you could use stoi directly without the middle step of splitting the string:
size_t idx = 0, input_len = input.size();
vector<int> values;
while (idx < input_len) {
size_t new_idx;
values.push_back(stoi(input.substr(idx, input_len - idx), &new_idx));
idx += new_idx;
for(; !isdigit(input[idx]) && input[idx] != '-'; ++idx); // skip to the next number
}
I have 2 arrays:
arr1 = [0, 1, 2, 3, 4, 5, 6]
arr2 = [0, 1, 3, 3, 4, 6]
I'm trying to write a function to compare the two arrays and make a new array with the missing numbers.
I tried this for loop but it didn't print out anything. What other ways can I compare 2 arrays of different sizes?
int n = 0;
int *newArr = new int[];
for (int i = 0; i<=6; i++) {
if (arr1[i] != arr2[i]) {
newArr[n] = arr1[i]
n++;
}
}
for (int j = 0; j<n; j++) {
cout << arr[j] << endl;
}
I'm trying to write a function to compare the two arrays and make a
new array with the missing numbers.
You can use a range-based for loop and std::find to check both arrays for the missing numbers
And then store the result in a std::set
Example
int main()
{
int a[] { 0, 1, 2, 3, 4, 5, 6 };
int b[] { 0, 1, 3, 3, 4, 6 };
std::set<int> c;
for (auto const& i : a)
if (std::find(std::begin(b), std::end(b), i) == std::end(b))
c.insert(i);
for (auto const& i : b)
if (std::find(std::begin(a), std::end(a), i) == std::end(a))
c.insert(i);
}
or using a regular for loop
for (auto it = std::begin(a); it != std::end(a); ++it)
if (std::find(std::begin(b), std::end(b), *it) == std::end(b))
c.insert(*it);
Try something like this:
int arr1[] = {0, 1, 2, 3, 4, 5, 6};
int size_arr1 = 7;
int arr2[] = {0, 1, 3, 3, 4, 6};
int size_arr2 = 6;
int *newArr = new int[size_arr1 + size_arr2];
int n = 0;
for (int i = 0; i < size_arr1; i++) {
newArr[n++] = arr1[i];
}
for (int i = 0; i < size_arr2; i++) {
bool found = false;
for (int j = 0; j < n; j++) {
if (arr2[i] == newArr2[j]) {
found = true;
break;
}
}
if (!found)
newArr[n++] = arr1[i];
}
for (int i = 0; i < n; i++) {
cout << newArr[i] << endl;
}
delete[] newArr;
Another way to collect unique numbers from both arrays is to use a std::set or std::unsorted_set instead, eg:
#include <unordered_set>
int arr1[] = {0, 1, 2, 3, 4, 5, 6};
int arr2[] = {0, 1, 3, 3, 4, 6};
set::unsorted_set newArr;
for(int num : arr1) {
newArr.insert(num);
}
for(int num : arr2) {
newArr.insert(num);
}
for (int num : newArr) {
std:: cout << num << endl;
}
You've declared the arrays dynamically in your code (i.e. on the heap) and initializing them with numbers isn't as straight forward. You are also allocating an array of 0 size. If you declare your arrays on the stack then you can achieve what you want to do with a nested for loop.
Take note though that for large datasets this would be inefficient. If you're curious research binary search algorithms.
#include <iostream>
int main()
{
int arr1Size = 6;
int arr2Size = 8;
int arr1[] = { 2, 5, 7, 12, 45, 65 };
int arr2[] = { 2, 5, 6, 10, 32, 65, 98, 123 };
for (int i = 0; i < arr1Size; i++)
{
for (int j = 0; j < arr2Size; j++)
{
if (arr1[i] == arr2[j])
{
std::cout << arr1[i] << std::endl;
}
}
}
}
I know using vectors is much easier but it just crossed my mind if i wanted to use C-style arrays to do generic matrix multiplication, how would that work. I searched online and got some help by using templates but the array won't return from the function.
// Example program
#include <iostream>
#include <string>
using namespace std;
template <typename T, size_t row1, size_t col1, size_t row2, size_t col2>
typename multiply(T(&a)[row1][col1], T(&b)[row2][col2]) {
if (row1 != col2) {
cout << "Error! Rows of first matrix is not the same as Columns of second "
"matrix, therefore Multiplication is NOT POSSIBLE!";
return 0;
} else {
T c[row1][col2];
for (size_t i = 0; i < row1; ++i) {
for (size_t j = 0; j < col2; ++j) {
c[i][j] = 0;
for (size_t k = 0; k < col1; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
}
int main() {
// C-style array
int my_array[2][5] = {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}};
int my_array2[5][2] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
int my_array3[2][2];
int c[2][2] = multiply(my_array, my_array2);
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
cout << c[i][j] << " ";
}
cout << endl;
}
return 0;
}
So any idea how I can make this code work?
You can't either return 0 either return c in your function as you do.
Instead, pass the output as a parameter c and put a and b as const.
The return of the function is the error code (0 for OK, -1 for KO)
Btw, your check condition is wrong, I corrected it also
// Example program
#include <iostream>
#include <string>
using namespace std;
template <typename T, size_t row1, size_t col1, size_t row2, size_t col2>
int multiply(const T(&a)[row1][col1], const T(&b)[row2][col2], T(&c)[row1][col2] ) {
if (col1 != row2) {
cout << "Error! Columns of first matrix is not the same as Rows of second "
"matrix, therefore Multiplication is NOT POSSIBLE!";
return -1;
} else {
//T c[row1][col2];
for (size_t i = 0; i < row1; ++i) {
for (size_t j = 0; j < col2; ++j) {
c[i][j] = 0;
for (size_t k = 0; k < col1; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return 0;
}
}
int main() {
// C-style array
int my_array[2][5] = {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}};
int my_array2[5][2] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
int my_array3[2][2];
int a = multiply(my_array, my_array2,my_array3);
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
cout << my_array3[i][j] << " ";
}
cout << endl;
}
return 0;
}
You can pass the result as a parameter:
template<class T,int NLine1,int NC1L2,int NCol2>
void multiply(T (left&)[NLine1][NC1L2], T(right&)[NC1L2][NCol2], T(result&)[NLine1][NCol2]);
So I have an array like {1, 4, 2, 3, 5, 3, 7} and I have to make a new array containing the sums of each 3 consecutive elements like {7, 9, 10, 11, 15}.
So far I got here and I don't know what I'm doing wrong.
#include <iostream>
#include <conio.h>
using namespace std;
int a[] = {1, 1, 2, 3, 5, 3, 7};
int lung = sizeof(a)/sizeof(a[0]);
int *l = new int[10];
int calc(int *a, int m)
{
int sum = 0;
int stmax = (lung - m) - 1;
for(int st=0;st <= stmax; st++)
{
for(int i = 0; i < m; i++)
{
sum = sum + a[st+i];
}
l[st] = sum;
}
return 0;
}
void main()
{
int a[] = {1, 1, 2, 3, 5, 3, 7};
cout << calc(a, 3)<< endl;
for (int i = 0; i < lung; i++)
{
cout << l[i] << " | ";
}
_getch();
}
you have to set sum=0; after each iteration
for(int st=0;st <= stmax; st++)
{
sum=0;
for(int i = 0; i < m; i++)
{
sum = sum + a[st+i];
}
l[st] = sum;
}
and also you have to declare int stmax = (lung - m); //ommit -1
and in the main function print the array till stmax
for (int i = 0; i <= stmax; i++)
{
cout << l[i] << " | ";
}
I would do it like this:
#include <iostream>
#include <conio.h>
using namespace std;
int a[] = { 1,4,2,3,5,3,7};
int lung = sizeof(a) / sizeof(a[0]);
int *l = new int[10];
void calc(int* a, int m){
a += 1; // Increment pointer
for (int st = 0; st <m; st++){
l[st] = a[st-1]+a[st]+a[st+1];
}
}
int main(){
calc(a, lung-2);
for (auto i = 0; i < lung-2; i++){
std::cout << l[i] << " ";
}
}