How do I change postition of number in 2d array in c++? - c++

I tried to find the biggest number in every row of a 2D array, and to put it on diametrum. I did that, but I now want to replace that number, not just declare them on diametrum. How do I change the place of numbers for my case? Here's code:
#include<iostream>
using namespace std;
int mac(int b[],int n)
{
int i,max,c=0;
max=b[0];
for(i=0;i<n;i++)
{
if(max<b[i]) {max=b[i];c=i;}
}
return c;
}
int main()
{
int i,j,n;
int c;
int a[10][10],b[10];
cin>>n;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
cin.ignore();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
b[j]=a[i][j];
c=mac(b,n);
/*Place where i should change something*/
a[i][i]=a[i][c];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j];
}
cout<<endl;
}
return 0;
}

You need to do swap. So you have to define one temporary variable to save actual value from [i][i]. Then overwrite position [i][i] with max value and at the end you need place temp value back to position where you found max value.
/*Place where i should change something*/
int temp = a[i][i];
a[i][i] = a[i][c];
a[i][c] = temp;

Assuming that when n = 3, and input is 1 2 3 4 5 6 7 8 9 the output should be 321 465 789
The code should be
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector< vector<int> > a;
size_t n;
cout<<"Enter number of value for N: ";
cin>>n;
a.reserve(n);
for (size_t i = 0; i<n;i++) {
a[i].reserve(n);
for (size_t j =0 ; j<n;j++) {
cin>>a[i][j];
}
}
for (size_t i = 0; i<n;i++) {
size_t index = 0;
int max = INT_MIN;
for (size_t j =0 ; j<n;j++) {
if (a[i][j] >= max) {
max = a[i][j];
index = j;
}
}
a[i][index] = a[i][i];
a[i][i] = max;
}
cout<<"\nOutput\n";
for (size_t i = 0; i<n;i++) {
for (size_t j =0 ; j<n;j++) {
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}

Related

Rotation of an array

This is a C++ code and I feel its correct, yet it doesn't work.
I will explain a bit for understanding
t - number of tries.
n - size of array.
k - number of rotation.
Input: 1 2 3 4 5 .
for k=2, Output: 4 5 1 2 3.
Please advise on the same.
#include<iostream>
using namespace std;
int main() {
int t,n,k;
cin >> t;
int s = 0;
int a[1000];
int b[1000];
for (int i = 0; i < t; i++) {
cin >> n; //TAKING IN THE NUMBER OF ELEMENTS.
cin >> k; // TAKING IN AMOUNT OF ROTATION.
for (int j = 0; j < n; j++) {
cin >> a[j]; //READING ARRAY.
cout << " ";
}
for (int y = 0; y < n; y++) {
b[y + k] = a[y]; // REARRANGING ARRAY.
if (y + k >= n) {
b[s] = a[y];
s++;
}
cout << b[y] << " "; // SHOWING ARRAY.
}
}
return 0;
}
The Problem with your code is in line,
cout<<b[y]<<" "; // SHOWING ARRAY.
You must take it out of that for loop,since you are trying to print something that you have not given value.For example,first y=0 you set b[y+2] but you print b[y] which is not yet set.
Another problem which you will see for t>1 is initialisation of s has been done out of main i.e it is 0 for only first run.
the final code will be
#include
using namespace std;
int main()
{
int t;
cin>>t;
int n;
int k;
int a[1000];
int b[1000];
for(int i=0;i<t;i++)
{
int s=0; //Changed
cin>>n;
cin>>k;
for(int j=0;j<n;j++)
{
cin>>a[j];
// cout<<" "; //Not needed
}
for(int y=0;y<n;y++)
{
b[y+k]=a[y];
if(y+k>=n)
{
b[s]=a[y];
s++;
}
//cout<<b[y]<<" "; // changed this
}
for(int y=0;y<n;y++) //added this
cout<<b[y]<<" ";
}
return 0;
}

wrong answer in kadanes algorithm

kadane's algorithm implementation
input = (t=1;n=3;arr={-1,4,5}) gives output 8 but expected output was 9.
#include <iostream>
#include<cmath>
using namespace std;
int Max( int *arr, int n){
int currmax = arr[0];
int globalmax = arr[0];
for(int i = 1; i < n; i++) {
currmax= max(currmax, currmax + arr[i]);
if(currmax > globalmax)
globalmax = currmax;
}
return globalmax;
}
int main() {
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++) cin >> arr[i];
cout << Max(arr, n) << " ";
}
}
Change currmax= max(currmax, currmax+arr[i]) to currmax= max(arr[i], currmax+arr[i]) as to compare current value with array value.
In you case -1, 4, 5
Value of currmax will be updated as below:
1. currmax = -1, currmax+arr[1] = 3 (compare btw(4,3))
2. currmax= 4 , currmax+arr[2] = 9 (compare btw(5,9))
3. currmax = 9
Code Below:
#include <iostream>
#include <cmath>
using namespace std;
int Max( int *arr, int n){
int currmax=arr[0];
int globalmax=arr[0];
for(int i=1;i<n;i++) {
currmax= max(arr[i], currmax+arr[i]); // your code line was wrong here check and try
if(currmax>globalmax)
globalmax=currmax;
}
return globalmax;
}
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++) cin>>arr[i];
cout<<Max(arr,n)<<" ";
}
}
Please find the correct Answer for Maximum SubArray (Kadane's Theorem)
C# Solution:
public int MaxSubArray(int[] nums) {
int maxSum = nums[0];
int currSum = 0;
for(int i = 0; i < nums.Length; i++){
currSum = currSum + nums[i];
if(maxSum < currSum){
maxSum = currSum;
}
if(currSum < 0){
currSum = 0;
}
}
return maxSum;
}
enter code here

Selection Sort in c++

i'm trying to understand selection sort from this video:
https://www.youtube.com/watch?v=79AB11J5BqU
this is my current code :
#include <iostream>
int main() {
int numbers[5]={5,3,4,1,2};
int temp;
std::cout<<"BEFORE SORT : \n";
for(int x=0;x<5;x++){
std::cout<<numbers[x]<<" ";
}
for (int i = 0; i < 5; ++i) {
for (int j = i+1; j < 5; ++j) {
if(numbers[j]<numbers[i]){
temp = numbers[j];
numbers[j] = numbers[i];
numbers[i] = temp;
}
}
}
std::cout<<"\n\nAFTER SORT : \n";
for(int x=0;x<5;x++){
std::cout<<numbers[x]<<" ";
}
}
Am i doing the selection sort just like the video?
or am i instead doing buble sort ?
Thanks
In selection sort you find a minimal (or maximal) element and put it to top (bottom), then repeat it again for the rest of list.
It would be a selection sort, but you don't need to do swap every number you compare to find the smallest one. Store smallest number index in each internal loop and do one swap at the end of it.
unsigned minIndex;
for (int i = 0; i < 5; ++i) {
minIndex = i;
for (int j = i + 1; j < 5; ++j) {
if(numbers[j] < numbers[minIndex]){
minIndex = j;
}
}
if (minIndex != i) { // Do swapping
temp = numbers[i];
numbers[i] = numbers[minIndex];
numbers[minIndex] = temp;
}
}
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning.
#include<iostream>
using namespace std;
// Selection Sort//
void Selection_Sort(int a[],int n)
{
for(int i=0;i<n-1;i++)
{
int min_index=i;
for(int j=i;j<n-1;j++)
{
if(a[j]<a[min_index]){
min_index=j;
}
}
swap(a[i],a[min_index]);
}
}
int main()
{
int n,key;
cin>>n;
int a[1000];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
Selection_Sort(a,n);
for(int i=0;i<n;i++)
{
cout<<a[i];
}
}

C++ sorting an array in ascending order Printing

Im trying to sort an array in ascending order and print it out and Im having trouble of where to put my cout in my code.
for (int k=0; k<ARRAY_SIZE; k++) {
for (int l=1; l<ARRAY_SIZE-1; l++) {
if(numbers[l] > numbers[k]) {
temp = numbers[k];
numbers[k] = numbers[l];
numbers[l] = temp;
}
cout<<numbers[k];
}
}
If you want to see the result of sorting you have to print the array in new for loop
Remove the actual cout and put it after the sorting loop, in a new loop
for(int k = 0 ; k < ARRAY_SIZE; ++k)
cout << numbers[k] << " ";
This is a program that finds the length of a number n and converts it to an array arr and sorts that array in ascending order.(possible aptitude question)
#include <iostream>
using namespace std;
int main()
{
int n,i=0,j,num,temp,len=0,arr[10]={0};
cin>>n;
num=n;
while(n!=0)
{
len++;
n/=10;
}
cout<<len<<endl;
for ( i = len; i >= 0; i--)
{
arr[i] = num%10;
num/=10;
}
for(i=1;i<=len;i++)
for(j=0;j<=len-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
for(i=1;i<=len;i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
try this :
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
//array declaration
int arr[] = {1,2,5,8,4,3};
int n,i,j;
n = sizeof(arr) / sizeof(arr[0]);
int temp;
//print input elements
cout<<"Unsorted Array elements:"<<endl;
for(i=0;i<n;i++)
if(arr[i] == arr[n - 1]){
cout<<arr[i]<<"";
}else{
cout<<arr[i]<<",";
}
cout<<endl;
//sorting - ASCENDING ORDER
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp =arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
//print sorted array elements
cout<<"Sorted (Ascending Order) Array elements:"<<endl;
for(i=0;i<n;i++)
if(arr[i] == arr[n - 1]){
cout<<arr[i]<<"";
}else{
cout<<arr[i]<<",";
}
cout<<endl;
return 0;
}

Using a function to remove duplicates from an array in C++

I'm writing a program that has a user input integers into an array, calls a function that removes duplicates from that array, and then prints out the modified array. When I run it, it lets me input values into the array, but then gives me a "Segmentation fault" error message when I'm done inputing values. What am I doing wrong?
Here is my code:
#include <iostream>
using namespace std;
void rmDup(int array[], int& size)
{
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (array[i] == array[j])
{
array[i - 1 ] = array[i];
size--;
}
}
}
}
int main()
{
const int CAPACITY = 100;
int values[CAPACITY], currentSize = 0, input;
cout << "Please enter a series of up to 100 integers. Press 'q' to quit. ";
while (cin >> input)
{
if (currentSize < CAPACITY)
{
values[currentSize] = input;
currentSize++;
}
}
rmDup(values, currentSize);
for (int k = 0; k < currentSize; k++)
{
cout << values[k];
}
return 0;
}
Thank you.
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (array[i] == array[j])
{
array[i - 1 ] = array[i]; /* WRONG! array[-1] = something */
size--;
}
}
}
If array[0] and array[1] are equal, array[0-1] = array[0], meaning that array[-1] = array[0]. You are not supposed to access array[-1].
I wouldn't make it even possible to create duplicates:
int main()
{
const int CAPACITY = 100;
cout << "Please enter a series of up to 100 integers. Press 'q' to quit. ";
std::set<int> myInts;
int input;
while (std::cin >> input && input != 'q' && myInts.size() <= CAPACITY) //note: 113 stops the loop, too!
myInts.insert(input);
std::cout << "Count: " << myInts.size();
}
And do yourself a favour and don't use raw arrays. Check out the STL.
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
vector<int> vec = {1,1,2,3,3,4,4,5,6,6};
auto it = vec.begin();
while(it != vec.end())
{
it = adjacent_find(vec.begin(),vec.end());
if(it != vec.end())
vec.erase(it);
continue;
}
for_each(vec.begin(),vec.end(),[](const int elem){cout << elem;});
return 0;
}
This code compiles with C++11.
#include<iostream>
#include<stdio.h>
using namespace std;
int arr[10];
int n;
void RemoveDuplicates(int arr[]);
void Print(int arr[]);
int main()
{
cout<<"enter size of an array"<<endl;
cin>>n;
cout<<"enter array elements:-"<<endl;
for(int i=0;i<n ;i++)
{
cin>>arr[i];
}
RemoveDuplicates(arr);
Print(arr);
}
void RemoveDuplicates(int arr[])
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;)
{
if(arr[i]==arr[j])
{
for(int k=j;k<n;k++)
{
arr[k]=arr[k+1];
}
n--;
}
else
j++;
}
}
}
void Print(int arr[])
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}