I have a problem, if you know how to solve it, please help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I have a problem, if you know how to solve it, please help

I have a task, and I can't complete it, I don't understand what needs to be added so that the subtraction occurs in a cycle, and not once. task: Create a new array of the same length as the source array, where the element in position X is equal to the difference of elements in positions X and X-a of the source array, where a is a number entered by the user. Elements with indexes less than a are copied into the result array. Example: for the array {1,2,7,4,9} with the entered number 2 the result will be {1,2,6,2,2} (ie 1, 2, 7-1, 4-2, 9-7) My attempt: #include <iostream> using namespace std; int main() { setlocale(LC_CTYPE, "ukr"); int a[10] = { 1,2,3,4,5,6,7,8,9,10 }; int c[10] = { 1,2,3,4,5,6,7,8,9,10 }; int b; cout << "enter a number: " << endl; cin >> b; cout <<"initial array: "<< endl; for (int i = 0; i < 10; i++) { if (b == a [i]) { c[i] = a[i - b]; } cout << c[i] << endl; } }

10th Dec 2022, 8:44 AM
Marjan Borodaki
2 Answers
+ 2
You need to compare positions or indexes. See 7-1 is a[2] - a[2-2] when I>=a where I=2, a=2 4-2 is a[3] - a[3-2] when I>=a where I=3, a=2...
10th Dec 2022, 9:19 AM
Jayakrishna 🇮🇳
0
you need to work on the if statement. Now you only check if b is equal to a[i], but that's not what we want. we want to find the numbers that are equal to or greater than b. Because numbers less than b just need to be copied into c[i]. c[i] = a[i - b]; Not right, you are missing something in the last part. remember you should subtract X-a from X to get the new value, here your only using X-a. If the second array should be created as an empty array, then we need an else statement to copy in the elements lower then b.
10th Dec 2022, 2:56 PM
Andreas Lann
Andreas Lann - avatar