Related
I was working on this problem:
Coding Problem
Problem Statement: Edit Distance
Problem Level: MEDIUM
Problem Description:
You are given two strings S and T of lengths M and N, respectively. Find the 'Edit Distance' between the strings.
Edit Distance of two strings is the minimum number of steps required to make one string equal to the other. In order to do so, you can perform the following three operations:
1. Delete a character
2. Replace a character with another one
3. Insert a character
Note :
Strings don't contain spaces in between.
Input format :
The first line of input contains the string S of length M.
The second line of the input contains the String T of length N.
Output format :
Print the minimum 'Edit Distance' between the strings.
Constraints :
0 <= M <= 10 ^ 3
0 <= N <= 10 ^ 3
Time Limit: 1 sec
Sample Input 1 :
abc
dc
Sample Output 1 :
2
Explanation to the Sample Input 1 :
In 2 operations we can make string T to look like string S.
First, insert character 'a' to string T, which makes it "adc".
And secondly, replace the character 'd' of string T with 'b' from the string S. This would make string T as "abc" which is also string S.
Hence, the minimum distance.
Sample Input 2 :
whgtdwhgtdg
aswcfg
Sample Output 2 :
9
I wrote the following code for this:
#include <iostream>
using namespace std;
int EditDistance(string s, string t, int **arr)
{
int i = s.size();
int j = t.size();
//Base Case
if (t.size() == 0 || s.size()==0)
{
return max (s.size(),t.size());
}
int x, y, z, min_num;
if (arr[i][j] != (-1))
{
return arr[i][j];
}
//Recursive Case
if (s[0] == t[0])
{
arr[i][j] = EditDistance(s.substr(1), t.substr(1), arr);
return arr[i][j];
}
else
{
x = 1 + EditDistance(s.substr(1), t, arr);
y = 1 + EditDistance(s, t.substr(1), arr);
z = 1 + EditDistance(s.substr(1), t.substr(1), arr);
min_num = min(x, min(y, z));
arr[i][j] = min_num;
}
return min_num;
}
int EditDistance(string s, string t)
{
int a = s.size() + 1; //rows
int b = t.size() + 1; //columns
int **arr = new int *[b];
for (int i = 0; i < b; i++)
{
arr[i] = new int[a];
}
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{ cout << "Value of i is " << i <<endl;
cout << "Value of j is " << j <<endl;
arr[i][j] = (-1);
cout << "Value of array is " << arr[i][j] <<endl;
}
}
return EditDistance(s, t, arr);
delete[]arr;
}
int main()
{
string s1, s2;
cout << "Enter the two strings" << endl;
cin >> s1 >> s2;
cout << EditDistance(s1, s2) << endl;
return 0;
}
However, when I tried to enter "abc" and "ab" as the strings, it gives segmentation fault at i=3 and j=0 at the line arr[i][j] = (-1) in the function int EditDistance(string s, string t). However, what I really couldn't get was that why am I receving a segmentation fault, when I have created a 2-d array of size 4*3, then accessing the element arr[3][0] shouldn't give me a segmentation fault. Can anyone help?
OK, the Levenshtein distance . . .
There is one semantic error. Otherwise the program is OK.
Then you do many style erros. And you are using new and raw pointers for owned memory. In C++ such raw pointers are considered as evil and should not be used. Under no circumstances.
Additionally you forgot the release the owned memory. Your delete statement is an unreachable code area. And it only deletes one dimension.
If you would use meaningful variable names, then you would see immediately your problem. See here.
int numberOfRows = s.size() + 1; //rows
int numberOfColumns = t.size() + 1; //columns
int **arr = new int *[numberOfColumns];
for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++)
{
arr[columnIndex] = new int[numberOfRows];
}
for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++)
{
for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++)
{ cout << "Value of i is " << rowIndex << endl;
cout << "Value of j is " << columnIndex << endl;
arr[rowIndex][j] = (-1);
cout << "Value of array is " << arr[rowIndex][columnIndex] <<endl;
}
}
You are simply mixing up rows and columns. The indices are twisted.
Correcting that and switching the language to C++, will give you the following, basically identical code:
#include <iostream>
#include <string>
#include <algorithm>
int EditDistanceRecursive(const std::string& stringLeft, const std::string& stringRight, int** arr)
{
size_t sizeOfStringLeft = stringLeft.size();
size_t sizeOfStringRight = stringRight.size();
//Base Case
if (stringLeft.empty() || stringRight.empty())
{
return std::max(stringLeft.size(), stringRight.size());
}
int x, y, z, min_num;
if (arr[sizeOfStringLeft][sizeOfStringRight] != (-1))
{
return arr[sizeOfStringLeft][sizeOfStringRight];
}
//Recursive Case
if (stringLeft[0] == stringRight[0])
{
arr[sizeOfStringLeft][sizeOfStringRight] = EditDistanceRecursive(stringLeft.substr(1), stringRight.substr(1), arr);
return arr[sizeOfStringLeft][sizeOfStringRight];
}
else
{
x = 1 + EditDistanceRecursive(stringLeft.substr(1), stringRight, arr);
y = 1 + EditDistanceRecursive(stringLeft, stringRight.substr(1), arr);
z = 1 + EditDistanceRecursive(stringLeft.substr(1), stringRight.substr(1), arr);
min_num = std::min(x, std::min(y, z));
arr[sizeOfStringLeft][sizeOfStringRight] = min_num;
}
return min_num;
}
int EditDistance(const std::string& stringLeft, const std::string& stringRight)
{
// Get the dimensions of the dp table
size_t numberOfRows = stringLeft.size() + 1; //rows
size_t numberOfColumns = stringRight.size() + 1; //columns
// Dynamically allocate 2d-array in old C-Style
int** arr = new int* [numberOfRows];
for (size_t rowIndex = 0; rowIndex < numberOfRows; rowIndex++)
{
arr[rowIndex] = new int[numberOfColumns];
}
// Initialize memory and show debug output
for (size_t rowIndex = 0; rowIndex < numberOfRows; rowIndex++)
{
for (size_t columnIndex = 0; columnIndex < numberOfColumns; columnIndex++)
{
std::cout << "Value of row is " << rowIndex << '\n';
std::cout << "Value of column is " << columnIndex << '\n';
arr[rowIndex][columnIndex] = (-1);
std::cout << "Value of array at row/column is " << arr[rowIndex][columnIndex] << '\n';
}
}
// Caluclate result
int result = EditDistanceRecursive(stringLeft, stringRight, arr);
// Release dynamic allocated memory
for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++)
delete [] arr[rowIndex];
delete [] arr;
// Inform Result to upper function
return result;
}
int main()
{
// Tell user what to do
std::cout << "Enter the two strings:\n";
// Get 2 strings
std::string stringLeft, stringRight;
std::cin >> stringLeft >> stringRight;
// Calculate and output distance
int result = EditDistance(stringLeft, stringRight);
std::cout << "\n\nResult. Distance is: " << result << '\n';
return 0;
}
I used meaningful variable names
Fixed minor semantic bugs
Made some minor style optimizations
Use fully qualified names everywhere
And if we get rid of C-Style, not at all recommended new and raw pointers, we get the following C++ code:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using Columns = std::vector<int>;
using Arr = std::vector<Columns>;
int EditDistanceRecursive(const std::string& stringLeft, const std::string& stringRight, Arr& arr)
{
size_t sizeOfStringLeft = stringLeft.size();
size_t sizeOfStringRight = stringRight.size();
//Base Case
if (stringLeft.empty() || stringRight.empty())
{
return std::max(stringLeft.size(), stringRight.size());
}
int x, y, z, min_num;
if (arr[sizeOfStringLeft][sizeOfStringRight] != (-1))
{
return arr[sizeOfStringLeft][sizeOfStringRight];
}
//Recursive Case
if (stringLeft[0] == stringRight[0])
{
arr[sizeOfStringLeft][sizeOfStringRight] = EditDistanceRecursive(stringLeft.substr(1), stringRight.substr(1), arr);
return arr[sizeOfStringLeft][sizeOfStringRight];
}
else
{
x = 1 + EditDistanceRecursive(stringLeft.substr(1), stringRight, arr);
y = 1 + EditDistanceRecursive(stringLeft, stringRight.substr(1), arr);
z = 1 + EditDistanceRecursive(stringLeft.substr(1), stringRight.substr(1), arr);
min_num = std::min(x, std::min(y, z));
arr[sizeOfStringLeft][sizeOfStringRight] = min_num;
}
return min_num;
}
int EditDistance(const std::string& stringLeft, const std::string& stringRight)
{
// Get the dimensions of the dp table
size_t numberOfRows = stringLeft.size() + 1; //rows
size_t numberOfColumns = stringRight.size() + 1; //columns
// Create and initialize 2d vector
Arr arr(numberOfRows, Columns(numberOfColumns, -1));
// Caluclate result
return EditDistanceRecursive(stringLeft, stringRight, arr);
}
int main()
{
// Tell user what to do
std::cout << "Enter the two strings:\n";
// Get 2 strings
std::string stringLeft, stringRight;
std::cin >> stringLeft >> stringRight;
// Calculate and output distance
int result = EditDistance(stringLeft, stringRight);
std::cout << "\n\nResult. Distance is: " << result << '\n';
return 0;
}
What a pity that nobody will read that . . .
I think you inverted the indices of the array subscripts. You create the array like [b][a], but then use it like it's [a][b].
Hi i'm the beginner of c++.
This time, I try to reverse Array 's element-order using dynamic Allocation Operator.
For example an array{1,2,3,4} will be rearranged {4,3,2,1} through calling function 'reverseArray'.
Everything works fine but somehow i got unwanted integer '-1' along with rearranged Array.
For example {-1,4,3,2,1}.
It means i did wrong and i really want to learn my fault.
Here is my code, please help me to figure out.
#include<iostream>
#include<new>
using namespace std;
void reverseArray(int [] , int);
int main(){
int num=0;
cout << "enter size of pointer : " ;
cin >> num ;
int *pointerArray = new int [num];
cout << "enter integer numbers in pointer" << endl;
for(int index = 0 ; index < num ; index++){
cin >> pointerArray[index];
}
reverseArray(pointerArray, num);
delete[] pointerArray;
return 0 ;
}
void reverseArray(int reverse[], int Size){
int*ptArray[Size];
cout << "the reverse order of entered numbers is : " << endl;
for(int index = 0 ; Size >= 0 ; index++) {
ptArray[index] = &reverse[Size];
cout << *ptArray[index] << " " ;
--Size;
}
return ;
}
This function
void reverseArray(int reverse[], int Size){
int*ptArray[Size];
cout << "the reverse order of entered numbers is : " << endl;
for(int index = 0 ; Size >= 0 ; index++) {
ptArray[index] = &reverse[Size];
cout << *ptArray[index] << " " ;
--Size;
}
return ;
}
does not make sense.
For starters variable length arrays
int*ptArray[Size];
is not a standard C++ feature.
Secondly in this loop
for(int index = 0 ; Size >= 0 ; index++) {
ptArray[index] = &reverse[Size];
cout << *ptArray[index] << " " ;
--Size;
}
there is access beyond the arrays.
For example let's assume that Size is equal to 1.
In this case within the ,loop we have
ptArray[0] = &reverse[1];
and then
ptArray[1] = &reverse[0];
However the only valid index for such arrays is 0.
It is unclear what you are trying to do.
If you want to reverse an array in place then the function can look like
void reverseArray( int a[], size_t Size )
{
for ( size_t i = 0; i < Size / 2; i++ )
{
// you can use the standard function std::swap here
int tmp = a[i];
a[i] = a[Size - i - 1];
a[Size - i - 1] = tmp;
}
}
And in main after calling the function you can output the reversed array .
Pay attention to that there is standard algorithm std::reverse that can do the rask.
You could just write
std::reverse( reverse, reverse + num );
Here is a demonstrative program.
#include <iostream>
#include <algorithm>
void reverseArray( int a[], size_t Size )
{
for ( size_t i = 0; i < Size / 2; i++ )
{
// you can use the standard function std::swap here
int tmp = a[i];
a[i] = a[Size - i - 1];
a[Size - i - 1] = tmp;
}
}
int main()
{
const size_t N = 5;
int *a = new int[5] { 1, 2, 3, 4, 5 };
for ( size_t i = 0; i < N; i++ ) std::cout << a[i] << ' ';
std::cout << '\n';
reverseArray( a, N );
for ( size_t i = 0; i < N; i++ ) std::cout << a[i] << ' ';
std::cout << '\n';
std::reverse( a, a + N );
for ( size_t i = 0; i < N; i++ ) std::cout << a[i] << ' ';
std::cout << '\n';
delete [] a;
return 0;
}
Its output is
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
In your code you access the array out-of-bounds on the very first iteration, because for an array of size Size valid indices are 0 up to Size-1
It is not clear why you create an array of pointers, and int*ptArray[Size]; is a variable lenght array (VLA) which is not standard C++. Further, you do not need to include <new>.
To print a dynamically sized (ie size is taken from input) array you would use a std::vector and a loop:
#include <vector>
#include <iostream>
int main(){
size_t size;
std::cout << "enter size: ";
std::cin >> size;
std::vector<int> data(size);
for (auto& element : data) std::cin >> element;
for (size_t i = 0; i < data.size(); ++i) {
std::cout << data[ data.size()-1-i ]; // first element printed is data[data.size()-1]
}
}
If you want to reverse the array, not just print it in reverse order, there is std::reverse in <algorithm>. The algorithm also works with dynamically allocated arrays:
#include <iostream>
#include <algorithm>
int main(){
size_t size;
std::cout << "enter size: ";
std::cin >> size;
int* data = new int[size];
for (size_t i=0; i<size; ++i) std::cin >> data[i];
std::reverse(data,data+size);
for (size_t i=0; i<size; ++i) std::cout << data[i];
delete [] data;
}
#include
using namespace std;
int main() {
int n;
cin>>n;
int temp=n;
int rem=0;
int i=0;
while (n>0) {
n= n/10;
i++;
}
int *arr = new int(i);
i=0;
while (temp>0) {
rem=temp%10;
arr[i]=rem;
i++;
temp= temp/10;
}
int t=0;
while(t<i) {
cout<<arr[t]<<" ";
t++;
}
return 0;
}
I need some help, I know this question was asked before but I don't get it and I cant solve it, so I need help. I need to move the elements of my array to a position to left. So if the input will be 1,2,3,4,5 then the output will be 2,3,4,5,1. I have done the same to right but to left I cant figure it out, please also explain the logic , thanks.
#include <iostream>
using namespace std;
int a[100],n,i,tempr,templ;
int main()
{
cin>>n;
for(i=1;i<=n;i++) cin >> a[i];
for(i=1;i<=n;i++)
{
tempr = a[n];
a[n] = a[i];
a[i] = tempr;
cout<<"Right: "<<a[i]<<endl;
}
for(i=1;i<=n;i++)
{
templ = a[2];
a[2] = a[i];
a[i] = templ;
cout<<"Left: "<<a[i]<<endl;
}
return 0;
}
Please help!
First problem is bad indexing:
for(i=1;i<=n;i++) cin >> a[i]; //wrong logic, C++ indexing start from 0
Correct approach:
for(i=0;i<n;i++) //all your loops
Second problem is wrong logic for shifting elements:
Corrected version:
//input example: 1 2 3 4 5
//to the left
int temp = a[0]; //remember first element
for(i=0;i<n-1;i++)
{
a[i] = a[i+1]; //move all element to the left except first one
}
a[n-1] = temp; //assign remembered value to last element
//output: 2 3 4 5 1
cout << "To left: " << endl;
for(i=0;i<n;i++)
cout << a[i] << endl;
//to the right
temp = a[n-1]; //remember last element
for(i=n-1;i>=0;i--)
{
a[i+1] = a[i]; //move all element to the right except last one
}
a[0] = temp; //assign remembered value to first element
//output: 1 2 3 4 5 because elements are shifted back by right shift
cout << "To right: " << endl;
for(i=0;i<n;i++)
cout << a[i] << endl;
EDIT:
How to display both shifts:
#include <iostream>
using namespace std;
int to_left[5], to_right[5],n,i,tempr,templ;
int main()
{
cout << "Input array size: ";
cin >> n;
for(i=0;i<n;i++)
{
cin >> to_left[i]; //read values to first array
to_right[i]=to_left[i]; //then copy values to second one
}
//shift first array to left
int temp = to_left[0];
for(i=0;i<n-1;i++)
{
to_left[i] = to_left[i+1]; //move all element to the left except first one
}
to_left[n-1] = temp; //assign remembered value to last element
//output: 2 3 4 5 1
cout << "To left: " << endl;
for(i=0;i<n;i++)
cout << to_left[i] << endl;
//shift second array to right
temp = to_right[n-1]; //remember last element
for(i=n-1;i>=0;i--)
{
to_right[i+1] = to_right[i]; //move all element to the right except last one
}
to_right[0] = temp; //assign remembered value to first element
//output: 1 2 3 4 5 because elements are shifted back by right shift
cout << "To right: " << endl;
for(i=0;i<n;i++)
cout << to_right[i] << endl;
return 0;
}
Note that your code look very much like C code. In C++, you can declare variables in any segment of code, not just at the beginning. In C++, you can declare variable in for loop like this: for(int i=0; i<...) - no need for global variable i
For reference, this would be good C++ code example that satisfies problem you are facing:
#include <iostream>
#include <vector>
int main()
{
std::size_t n; //size_t is unsiged type used for various sizes of containers or types
std::cout << "Input array size: ";
std::cin >> n;
std::vector<int> to_left(n), to_right(n); //two dynamic arrays containing integers, takin n as their size
for(std::size_t i=0;i<to_left.size();++i) //use vector size(), instead of n, also ++i in considered better for loops that i++ (may be faster)
{
std::cin >> to_left[i];
to_right[i]=to_left[i];
}
int temp = to_left[0]; //declare temp here, not at the begining of code
for(std::size_t i=0;i<n-1;++i)
to_left[i] = to_left[i+1];
to_left[n-1] = temp;
std::cout << "To left: " << std::endl;
for(std::size_t i=0;i<n;++i)
std::cout << to_left[i] << std::endl;
temp = to_right[n-1]; //reuse temp
for(int i=to_right.size()-1;i>=0;--i) //note int, not std::size_t, because size_t is always >=0, loop would never end.
to_right[i+1] = to_right[i];
to_right[0] = temp;
std::cout << "To right: " << std::endl;
for(std::size_t i=0;i<n;i++)
std::cout << to_right[i] << std::endl;
return 0;
}
And here would be ideal C++ code:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::size_t n;
std::cout << "Input array size: ";
std::cin >> n;
std::vector<int> to_left(n), to_right(n);
for(std::size_t i=0;i<to_left.size();++i)
{
std::cin >> to_left[i];
to_right[i]=to_left[i];
}
// rotate first array to the left
std::rotate(to_left.begin(), to_left.begin() + 1, to_left.end());
// rotate second array to right
std::rotate(to_right.rbegin(), to_right.rbegin() + 1, to_right.rend());
std::cout << "To left:" << std::endl;
for(auto x : to_left) //C++11 feature, x iterates through container
std::cout << x << std::endl;
std::cout << "To right:" << std::endl;
for(auto x : to_right)
std::cout << x << std::endl;
return 0;
}
Or you can use memmove(...) projected exactly for those purpose, here your sample:
#include <iostream>
#include <cstring>
using namespace std;
//rotate Left
void r_left(int *a,int n)
{
int tmp=a[0];
memmove(a,a+1,sizeof(int)*(n-1));
a[n-1]=tmp;
}
//rotate right
void r_right(int *a,int n)
{
int tmp=a[n-1];
memmove(a+1,a,sizeof(int)*(n-1));
a[0]=tmp;
}
void show(int *a,int n)
{
while(n--)
cout<<*a++<<' ';
cout<<endl;
}
int main()
{
int ar[]={1,2,3,4,5};
int n=sizeof(ar)/sizeof(ar[0]);
r_left(ar,n);
show(ar,n);
r_right(ar,n);
show(ar,n);
return 0;
}
easiest way to swap elements in C++ is to use std::iter_swap()
so for an array of 4 elements to swap elements 1 and 4 you would do the following
int a[4];
std::iter_swap(a, a+3);
note that you also need to #include <algorithm> for this to work
the basic logic of the function is that you give the location in memory of the 2 elements, so as the first element of an array is also its location in memory, you can pass a + n, when n is equal to the n-1 index number of the element you want to swap
As other already have stated it's all about indices. In a for-loop you are almost always in trouble if your stop condition is i <= size, because arrays in C++ are zero-indexed.
Where Black Moses alogrithm is far the easiest to understand (and probably the fastes), I read your code as if you try to swap the first value of the array through the array to the last position. Below I have tried to pin out this approach.
#include <stdio.h>
#include <tchar.h>
#include <iostream>
void ShiftLeft(int* pArr, size_t length)
{
for (size_t i = 1; i < length; i++)
{
int tmp = pArr[i - 1]; // Preserves the previous value
pArr[i - 1] = pArr[i]; // Overwrites the previous position with the current value
pArr[i] = tmp; // Stores the previous value in the current position
// All in all the first value is swapped down the array until it is at the length - 1 position
// and all the other values are swapped to the left.
/* For an array with 4 values the progression is as follows:
i = 0: 1 2 3 4
i = 1: 2 1 3 4
i = 2: 2 3 1 4
i = 3: 2 3 4 1
*/
}
}
void ShiftRight(int* pArr, size_t length)
{
for (size_t i = length - 1; i > 0; i--)
{
// This code does exactly the same as for ShiftLeft but the loop is running backwards
int tmp = pArr[i - 1];
pArr[i - 1] = pArr[i];
pArr[i] = tmp;
}
}
void Print(int* pArr, size_t length)
{
for (size_t i = 0; i < length; i++)
{
std::cout << pArr[i] << " ";
}
std::cout << std::endl;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
size_t length = sizeof(arr) / sizeof(arr[0]);
Print(arr, length);
ShiftLeft(arr, length);
Print(arr, length);
ShiftRight(arr, length);
Print(arr, length);
return 0;
}
#include <iostream>
using namespace std;
int a[100], outR[100], outL[100], n, i;
int main() {
cin >> n;
for (i = 0; i < n; i++) cin >> a[i];
// Right
for (i = 0; i < n; i++) {
outR[i+1]= a[i];
}
outR[0] = a[n-1]; // add first number
// Left
for (i = 1; i < n; i++) {
outL[i-1]= a[i];
}
outL[n-1] = a[0]; // add last number
// Answer
cout << "Right:\n";
for(i=0; i<n; i++) {
cout << outR[i] << endl;
}
cout << "Left:\n";
for(i = 0; i < n; i++) {
cout << outL[i] << endl;
}
return 0;
}
Simple answer where you can easily see everything, good luck.
You may be interested in ,,vector coding", it seems be easier if you spend some time on this:
#include <iostream>
#include <vector>
using namespace std;
vector <int> a, outR, outL;
size_t i;
int main () {
int n, temp_int;
cin >> n;
while (n--) {
cin >> temp_int; // here you read number to your vector
a.push_back(temp_int); // here you add this to vector
// remember that vector start from element 0 as like arrays
}
// Left
// remember that last element will be first
// you may have acces to size of your vector easily
for (i = 0; i < (a.size()-1); i++) {
outL.push_back(a.at(i+1)); // here you create new vector
}
outL.push_back(a.at(0)); // add last elemet which rotated
// Right
// to rotate left first you have push last element so
outR.push_back(a.at(a.size()-1)); // add first elemet which rotated
for (i = 1; i < a.size(); i++) {
outR.push_back(a.at(i-1)); // here you push rest
}
cout << "Left" << "\n";
for (i = 0; i < a.size(); i++) {
cout << outL.at(i) << endl; // here you print value
}
cout << "Right" << "\n";
for (i = 0; i < a.size(); i++) {
cout << outR.at(i) << endl; // here you print value
}
return 0;
}
int* leftShiftOneByOneWIthoutTemp(int arr[], int sz)
{
for (int i=0 ;i < sz-1; i++)
{
arr[i] = arr[sz-1] + arr[i];
arr[sz-1] = arr[i] - arr[sz-1] ;
arr[i] = arr[i] - arr[sz-1] ;
std::cout << "iter "<< i << std::endl;
printArray(arr,5);
}
std::cout << "final "<< std::endl;
printArray(arr,5);
return arr;
}
Replace your code (to shift array left) with below code.
templ = a[0];
for(i=0;i<n-1;i++)
{
a[i] = a[i+1];
cout<<"Left: "<<a[i]<<endl;
}
a[n-1] = templ;
cout<<"Left: "<<a[n-1]<<endl;
I am trying to make a program that recieves numbers from the user, and then rearranges the from least to greatest. I am using vectors (which I just learned about), and it gives me a subscript out of range error. I am not able to find what part of the code gives me this error, so hopefully someone more knowledgeable on vector and c++ can find it:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void order(int a, int b);
void orderRev(int a, int b);
int main() {
vector<int> num;
bool going = true;
do {
cout << "\nEnter a number or type 'x' to order:" << endl;
string reply;
getline(cin, reply);
if (reply != "x") {
int a = atoi(reply.c_str());
num.push_back(a);
cout << "\nYou currently have " << num.size() << " numbers added." << endl;
}
else {
going = false;
}
} while (going);
for (int i = 0; i < num.size(); i++) {
order(num[i], num[i + 1]);
}
for (int i = num.size() - 1; i >= 0; i--) {
orderRev(num[i + 1], num[i]);
}
cout << "\nThe number you entered in order from least to greatest are: " << endl;
for (int i = 0; i < num.size(); i++) {
cout << num[i] << " ";
}
void order(int a, int b) {
if (a > b) {
int c = b;
b = a;
a = c;
}
}
void orderRev(int a, int b) {
if (a < b) {
int c = b;
b = a;
a = c;
}
}
Fix these lines to this:
// added the -1 as this will now go up to the 2nd to last element
// for `n`, and the last element for `n+1`
for (int i = 0; i < num.size() - 1; i++) {
order(num[i], num[i + 1]);
}
// changed the starting number to size -2 (for the same reasoning)
for (int i = num.size() - 2; i >= 0; i--) {
orderRev(num[i + 1], num[i]);
}
Why does this need to be this way? Think about how indices in C++ work. They are zero-indexed! That means that if you want both the element and the one in front of it, you must go up to the size of the vector minus 1. Hence, for a vector of 10 items (size 10), at i == 9 your code will work like this:
for (int i = 0; i < num.size(); i++) {
// i = 9
order(num[9], num[9+1]);// index 10 does not exist! Hence, you really need to go up to num.size() - 1!
}
Vectors index start with 0. index will be 0 to n-1 , if you use num[i + 1] it will exceed the vector size, if you don't check in loop condition.
Your code has more than one flaw. The output will be same as the input , hint: know the difference between pass by reference and pass by value and after that check some sorting algorithms.
We had a project that asked us to Write a program that allows a user to enter a series of numbers "read numbers into an array for further processing, user signals that they are finished by entering a negative number (negative not used in calculations), after all numbers have been read in do the following, sum up the #'s entered, count the #'s entered, find min/max # entered, compute average, then output them on the screen. So the working version of this that I made looks like so
/* Reads data into array.
paramater a = the array to fill
paramater a_capacity = maximum size
paramater a_size = filled with size of a after reading input. */
void read_data(double a[], int a_capacity, int& a_size)
{
a_size = 0;
bool computation = true;
while (computation)
{
double x;
cin >> x;
if (x < 0)
computation = false;
else if (a_size == a_capacity)
{
cout << "Extra data ignored\n";
computation = false;
}
else
{
a[a_size] = x;
a_size++;
}
}
}
/* computes the maximum value in array
paramater a = the array
Paramater a_size = the number of values in a */
double largest_value(const double a[], int a_size)
{
if(a_size < 0)
return 0;
double maximum = a[0];
for(int i = 1; i < a_size; i++)
if (a[i] > maximum)
maximum = a[i];
return maximum;
}
/* computes the minimum value in array */
double smallest_value(const double a[], int a_size)
{
if(a_size < 0)
return 0;
double minimum = a[0];
for(int i = 1; i < a_size; i++)
if (a[i] < minimum)
minimum = a[i];
return minimum;
}
//computes the sum of the numbers entered
double sum_value(const double a [], int a_size)
{
if (a_size < 0)
return 0;
double sum = 0;
for(int i = 0; i < a_size; i++)
sum = sum + a[i];
return sum;
}
//keeps running count of numbers entered
double count_value(const double a[], int a_size)
{
if (a_size < 0)
return 0;
int count = 0;
for(int i = 1; i <= a_size; i++)
count = i;
return count;
}
int _tmain(int argc, _TCHAR* argv[])
{
const int INPUT_CAPACITY = 100;
double user_input[INPUT_CAPACITY];
int input_size = 0;
double average = 0;
cout << "Enter numbers. Input negative to quit.:\n";
read_data(user_input, INPUT_CAPACITY, input_size);
double max_output = largest_value(user_input, input_size);
cout << "The maximum value entered was " << max_output << "\n";
double min_output = smallest_value(user_input, input_size);
cout << "The lowest value entered was " << min_output << "\n";
double sum_output = sum_value(user_input, input_size);
cout << "The sum of the value's entered is " << sum_output << "\n";
double count_output = count_value(user_input, input_size);
cout << "You entered " << count_output << " numbers." << "\n";
cout << "The average of your numbers is " << sum_output / count_output << "\n";
string str;
getline(cin,str);
getline(cin,str);
return 0;
}
That went fine, the problem I am having now is part 2. Where we are to "copy the array to another and shift an array by N elements". I'm not sure where to begin on either of these. I've looked up a few resources on copying array's but I was not sure how to implement them in the current code I have finished, especially when it comes to shifting. If anyone has any thoughts, ideas, or resources that can help me on the right path it would be greatly appreciated. I should point out as well, that I am a beginner (and this is a beginners class) so this assignment might not be the 'optimal' way things could be done, but instead incorporates what we have learned if that makes sense.
for(int i = 0; i < n; ++i){
int j = (i - k)%n;
b[i] = a[j];
}
Check it. I'm not sure
If this works you could improve it to
for(int i = 0; i < n; ++i)
b[i] = a[(i - k)%n];//here can be (i +/- k) it depends which direction u would shift
If you only want to copy the array into another array and shift them
ex : input = 1, 2, 3, 4, 5; output = 3, 4, 5, 1, 2
The cumbersome solution is
//no template or unsafe void* since you are a beginner
int* copy_to(int *begin, int *end, int *result)
{
while(begin != end){
*result = *begin;
++result; ++begin;
}
return result;
}
int main()
{
int input[] = {1, 2, 3, 4, 5};
size_t const size = sizeof(input) / sizeof(int);
size_t const begin = 2;
int output[size] = {0}; //0, 0, 0, 0, 0
int *result = copy_to(input + begin, input + size - begin, output); //3, 4, 5, 0, 0
copy_to(input, input + begin, result); //3, 4, 5, 1, 2
return 0;
}
How could the stl algorithms set help us?
read_data remain as the same one you provided
#include <algorithm> //std::minmax_element, std::rotate_copy
#include <iostream>
#include <iterator> //for std::begin()
#include <numeric> //for std::accumulate()
#include <string>
#include <vector>
int main(int argc, char *argv[]) //don't use _tmain, they are unportable
{
const int INPUT_CAPACITY = 100;
double user_input[INPUT_CAPACITY];
int input_size = 0;
double average = 0;
cout << "Enter numbers. Input negative to quit.:\n";
read_data(user_input, INPUT_CAPACITY, input_size);
auto const min_max = std::minmax_element (user_input, user_input + input_size); //only valid for c++11
std::cout << "The maximum value entered was " << min_max.second << "\n";
std::cout << "The lowest value entered was " << min_max.first << "\n";
double sum_output = std::accumulate(user_input, user_input + input_size, 0);
cout << "The sum of the value's entered is " << sum_output << "\n";
//I don't know the meaning of you count_value, why don't just output input_size?
double count_output = count_value(user_input, input_size);
cout << "You entered " << count_output << " numbers." << "\n";
cout << "The average of your numbers is " << sum_output / count_output << "\n";
int shift;
std::cout<<"How many positions do you want to shift?"<<std::endl;
std::cin>>shift;
std::vector<int> shift_array(input_size);
std::rotate_copy(user_input, user_input + shift, user_input + input_size, std::begin(shift_array));
//don't know what are they for?
std::string str;
std::getline(std::cin,str);
std::getline(std::cin,str);
return 0;
}
if your compiler do not support c++11 features yet
std::minmax_element could replace by
std::min_element and std::max_element
std::begin() can replace by shift_array.begin()
I don't know what is the teaching style of your class, in my humble opinion, beginners should
start with those higher level components provided by c++ like vector, string, algorithms
and so on.I suppose your teachers are teaching you that way and you are allowed to use the
algorithms and containers come with c++(Let us beg that your class are not teaching you "c with classes" and say something like "OOP is the best thing in the world").
ps : You could use vector to replace the raw array if you like