For loops and addition - c++

So I'm trying to use a for loop to fill the array with the numbers 1-8. Then add:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + numb = x
And then save it to an variable called x. I've done filling the array, but I don't know how to calculate the summary of the array + the number you enter. Would appreciate some help a lot.
#include <iostream>
using namespace std;
int main()
{
int array[8];
int numb;
int x; // x = summary of array + numb;
cin >> numb; // insert an number
for (int i = 0; i <= 8; i++)
{
array[i]=i+1;
}
for (int i = 0; i < 8; i++)
{
cout << array[i] << " + ";
}
}

Change the last part to:
x = numb;
for (int i = 0; i < 8; i++)
{
x = x + array[i];
}
cout<<x<<endl;
Realistically though, if you wanted to add the first n whole numbers, there's a formula:
(n*(n+1))/2;
so your whole program would be:
#include <iostream>
using namespace std;
int main()
{
int n = 8;
int numb;
cin >> numb; // insert an number
int x = n*(n+1)/2+numb;
cout<<x<<endl;
}

For the initial loop, remove the =:
for (int i=0; i<8; i++) { array[i]=i+1; }
For adding all the elements of an array, then adding numb:
var x=0;
for (int i=0; i<8; i++) { x += array[i]; }
x+=numb;
Then you can cout you x variable.

Unless you're required to use for loops and arrays (e.g., this is homework), you might want to consider code more like:
std::vector<int> array(8);
// fill array:
std::iota(begin(array), end(array), 1);
int numb;
std::cin >> numb;
int total = std::accumulate(begin(array), end(array), numb);
std::cout << "Result: " << total;

Related

In Cpp for is not working properly from the function

I need to do this exercise, with struct and include the function in the struct to do the calculations, but for some reason, it's not working.
I recommended you to check the image for a better idea.
#include <iostream>
using namespace std;
struct Alfa{
double h,x,n,a;
void shuma(){
cout << "enter n: "; cin >> n;
cout << "enter a: "; cin >> a;
for (int i = 1; i >= n; i++){
x = 2 * i + a;
}
};
};
int main() {
Alfa alf;
alf.shuma();
alf.h = (alf.x / 2) + 3;
cout << alf.h;
return 0;
}
You are not following the formula in the image. Use this instead:
double sum = 0;
for (int i = 1; i <= n + 1; i++) {
if (i != 4) {
sum += 2 * i + a;
}
}
h = x / 2 + 3 * sum;
for (int i = 1; i >= n; i++) means i initiated to 1, while i>=n do the loop, then inc i. So when n is bigger then 1, it will never enter the loop. Maybe you want for (int i = 1; i <= n+1; i++) ?

Right rotation operation

int main()
{
int shiftSteps, newposition;
int numbers[10], numberscopy[10];
cin >> shiftSteps;
for (int i = 0; i < 10; i++)
cin >> numbers[i];
for (int i = 0; i < 10; i++)
numberscopy[i] = numbers[i];
//----------------------------------------
for (int i = 0; i < 10; i++)
{
newposition = (i + shiftSteps) % 10;
numbers[newposition] = numberscopy[i];
}
for (int i = 0; i < 10; i++)
cout << numbers[i] << " ";
}
I wrote this code to rotate 10 numbers to Right with auxiliary array "numberscopy", but i want to rewrite the code without auxiliary array and i don't know how.
You can rotate the array in-place, i.e. without using an auxiliary array with std::rotate which is a standard algorithm.
std::rotate(numbers, numbers + shiftSteps, numbers + 10);
Without auxiliary array, you can make use of reversal algorithm to rotate array by "shiftSteps".
It involves following three steps,
reversing array from 0 to sizeOfArray-1
reversing array from 0 to shiftSteps-1
reversing array from shiftSteps to sizeOfArray-1
Here's final code,
void reverseArray(int numbers[], int begin, int end)
{
while (begin < end)
{
swap(numbers[begin], numbers[end]);
begin++;
end--;
}
}
void rightRotate(int numbers[], int shiftSteps, int sizeOfArray)
{
reverseArray(numbers, 0, sizeOfArray-1);
reverseArray(numbers, 0, shiftSteps-1);
reverseArray(numbers, shiftSteps, sizeOfArray-1);
}
int main()
{
int shiftSteps;
int numbers[10];
cin >> shiftSteps;
for (int i = 0; i < 10; i++)
cin >> numbers[i];
rightRotate(numbers, shiftSteps, 10);
for (int i = 0; i < 10; i++)
cout << numbers[i] << " ";
}
Source for more info.

Why is this not printing the final output of sum?

Given an array of N integers. My task is to print the sum of all of the integers.
Input:
First line of input file contains a single integer T which denotes the number of test cases. For each test case, there will be two lines. First line contains N which denotes number of elements in an array, and second line contains N space seperated integers.
Output:
Corresponding to each test case, print the sum of array in a new line.
Constraints:
1 <= T <= 100
1 <= N <= 1000
0 <= Arr[i] <= 200
#include <iostream>
using namespace std;
int main()
{
int n, no_of_elem_array;
int arr[50], sum[50];
cin >> n;
int j = 0;
while (n--) {
cin >> no_of_elem_array;
for (int i = 0; i < no_of_elem_array; i++) {
cin >> arr[i];
}
for (int i = 0; i < no_of_elem_array; i++) {
sum[j] = sum[j] + arr[i];
}
j++;
}
for (int i = 0; i < n; i++) {
cout << sum[i] << endl;
}
return 0;
}
Output
2
4
1 2 3 4
6
5 8 3 10 22 45
There is n in your final loop
for(int i=0; i<n; i++){
cout<<sum[i]<<endl;
}
which is become 0 in
while(n--)
that is why it is not print anything
That is not how one adds arrays in C++:
This is the code I propose:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> vec_of_ints;
std::cout << "Enter the size of the array: ";
unsigned size = 0;
std::cin >> size;
vec_of_ints.resize(size);
for(auto& integer : vec_of_ints) {
std::cin >> integer;
}
std::cout << std::accumulate(vec_of_ints.begin(), vec_of_ints.end(), 0);
return 0;
}
Also, as #Alan Birtles has suggested, there is another (and better) alternative:
you do not need to store the input at all:
int main() {
unsigned size = 0;
std::cin >> size;
long sum = 0;
for (int i = 0; i < size; i++) {
int num = 0;
std::cin >> num;
sum += num;
}
std::cout << sum << "\n";
return 0;
}
Two issues:
Your arrays are not big enough, so there will be invalid access, which is undefined behaviour.
Array sum is local variable, so the value is uninitialized (i.e. It contains arbitrary values when allocated), you need to set them to zero by yourself.

Replace element in array by average

I have a question about the exercise from my course:
Write a program that takes array of real numbers as parameter and replaces each element that is smaller than average of the first and last element, by this average. This is my code:
#include <iostream>
#include <string>
using namespace std;
void replaverage(int arr[], int n)
{
for (int i; i < 6; i++) {
cout << "Enter the numbers" << endl;
cin >> arr[i];
}
int f = arr[0];
int l = arr[n - 1];
double av = f + l / 2;
for (int i; i < n; i++) {
if (arr[i] < av) {
arr[i] = av;
}
}
}
int main()
{
int n;
int arr[n];
replaverage(arr, n);
cout << arr << " " << endl;
return 0;
}
Code is working, however as an output, it is giving some kind of address "0x7fff2306a5c0". I'm beginner so I apologize, maybe my error is stupid but I don't know how to fix it. Thanks for helping!
#include <iostream>
#include <string>
using namespace std;
void replaverage(int arr[], int n)
{
for (int i = 0; i < n; i++) {
cout << "Enter the number: ";
cin >> arr[i];
cout << endl;
}
int f = arr[0];
int l = arr[n - 1];
double av = (f + l) / 2;
for (int i = 0; i < n; i++) {
if (arr[i] < av) {
arr[i] = av;
}
}
}
int main()
{
int n = 6; // Making 6 since you had it hardcoded
int arr[n];
replaverage(arr, n);
for (int i = 0; i < n; i++) {
cout << arr[i] << endl;
}
return 0;
}
First problem: Initialize your loop counters to 0;
Second problem: Initialize n in main being passed as parameter to
something
Third problem: Your average calculation is incorrect. It should be (f+l) / 2. Otherwise it will be doing l/2 + f, which is incorrect.
Fourth problem: You need to loop over your array to see all the
elements

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;
}