So I need help with array of 20 elements.
I need to find each element Absolute value, and if it's value is less than 1 then remove this element from array. Afterwards at the end of array put 0 in place of a removed element.
So im looking for some functions which will help me to do this.
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int OddSumm(int arr[], int lenght)
{
int i,p,z;
p=0;
for (i=0; i<lenght; i++)
if(arr[i]%2!=0 && arr[i]!=0 ){
p+=arr[i];
}
return p;
}
int SummBetweenNegativeNumbers(int arr[], int lenght) {
int s,i,r,z;
s=0;
for (i=0; i<lenght; i++)
if(arr[i]<0){
i++;
r=i;
for (i=r; i<lenght; i++)
if(arr[i]<0){
--i;
z=i;
for (i=r; i<=z; i++)
s+=arr[i];
}
}
return s;
}
int main(int argc, char *argv[])
{
int var;
int i,arr[20];
int s,p,lenght;
lenght=20;
{
printf("Type in %d elements \n",lenght);
for (i=0; i<lenght; i++)
scanf("%d", &arr[i]);
}
p=OddSumm(arr,lenght);
s=SummBetweenNegativeNumbers(arr,lenght);
printf("\n\t 1a)Odd numbers summ: %d",p);
printf("\n\t 1b)Elements summ between negativ numbers: %d", s);
printf("\n\t 2)Compressed array: ");
for(i=lenght-1; i>=0; i--)
printf(" %d",arr[i]);
printf("\n\n");
system("pause");
return 0;
}
use std::transform to transform your data
use std::remove to compact these nonzeros
use std::fill to fill padding zeros
example code
#include <vector>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#include <iostream>
int my_function(int value){
value = std::abs(value);
return value<1 ? 0:value;
}
int main()
{
int v[]={0,1,2,-4,-5,3};
std::transform(std::begin(v),std::end(v),std::begin(v),my_function);
auto end=std::remove(std::begin(v),std::end(v),0);
std::fill(end,std::end(v),0);
for(auto value : v)std::cout<<value<<' ';
}
Related
I tried to write a simple code to calculate an array elements' sum. every thing looks normal but the function return the sum value wrongly (it always multiply it by two). Although if I want just print the value, it works fine.
this is the code:
#include <iostream>
using namespace std;
void getElements(int[],int);
int sumOfElements(int[],int);
int number;
int sum=0;
int main()
{
int a[10];
getElements(a,5);
sumOfElements(a,5);
cout<<"The sum is "<<sumOfElements(a,5)<<endl;
return 0;
}
//Getting array's elements
void getElements(int numbers[],int size_)
{
for (int i=0; i<size_; i++)
{
cout<<"numbers["<<i<<"]: ";
cin>>number;
numbers[i]=number;
}
cout<<'\n';
}
//Calculation the sum of array's elements
int sumOfElements(int numbers[],int size_)
{
for(int i=0;i<size_;i++)
{
sum+=numbers[i];
}
cout<<sum<<endl;
return sum;
}
any idea? thank you in advance!
You defined int sum globally and were calling sumOfElementstwice, so sum contained twice what you expected.
Here is a modified version of your code that does what you want:
#include <iostream>
using namespace std;
void getElements(int[], int);
int sumOfElements(int[], int);
int main() {
int numbers[5];
getElements(numbers, 5);
cout << sumOfElements(numbers, 5);
return 0;
}
void getElements(int numbers[], int size) {
for (int i = 0; i < size; i++) {
cin >> numbers[i];
}
}
int sumOfElements(int numbers[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += numbers[i];
}
return sum;
}
Here is a modified and simpler version of your program:
#include <array>
#include <iostream>
#include <numeric>
using namespace std;
int main(){
const int num_elements_to_sum = 5;
array<int, num_elements_to_sum> elements;
for(int i=0; i<num_elements_to_sum; ++i){
cin>>elements[i];
}
int sum = accumulate(elements.begin(), elements.end(), 0);
cout<<"Sum: "<<sum<<endl;
return 0;
}
C++ has a dedicated fixed size array container, use this instead of C-style arrays. This then allows to use standard library algorithms instead of your own implementation (e.g. accumulate).
*Buatlah program untuk menghitung perkalian deret bilangan genap membentuk segitiga siku terbalik dengan hasil seperti pada gambar di atas.
my program is like this :
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, const char *argv[])
{
int i, j, n;
for(i=0; i<5; puts(""),++i)
{
n=0;
for(j=5; j>i; n+=2*(j--))
{
if(j>i+1) {
printf("%d * ",2*j);
}
else {
printf("%d ",2*j);
}
}
printf("\t= %d",n);
}
printf("\t\t110");
return(0);
}
how do I make the program exactly like in the picture above?
This code gives the output you stated in your question. I'm not sure though if you are intended to do it like this, since there might be a smarter solution using iomanip and iostream, because you included it.
#include <stdio.h>
using namespace std;
int main(int argc, const char *argv[])
{
int i, j, n;
for(i=0; i<5; puts(""),++i)
{
n=0;
for(j=5; j>i; n+=2*(j--))
{
if(j>i+1) {
printf("%d + ",2*j);
}
else {
printf("%d ",2*j);
}
}
for (int k = 0; k <= i; k++) printf(" ");
printf("= %d",n);
}
printf("\t\t ---------- +\n");
printf("\t\t\t 110\n");
return(0);
}
#include <iostream>
using namespace std;
int main(int argc, const char *argv[]){
int i, k, n, s=0;
for(k=2; k <= 10; k+=2) {
n=0;
for(i=10; k <= i; i-=2) {
if (k < i)
cout<<i<<" + " ;
else {
cout<< i;
cout.width(k*2);
cout<< right<< " = ";
}
n+=i;
}
cout<<n<<"\n";
s += n;
}
cout<< "------------------------- +"<<"\n";
cout<< " "<<s<<"\n";
return 0;
}
This is a code I wrote for bubble sort. I gave a comment //this line due to which I'm unable to run this program. Every time the first element of the array needs to be stored in 'temp'.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[7]={7,8,5,2,4,6};
int temp;
for(int i=0;i<7;i++)
{
temp=arr[0]; //this line.
for(int j=0;j<7-i;j++)
{
if(temp<arr[j])
temp=arr[j];
else
swap(arr[j],arr[j-i]);
}
}
for(int k=0;k<7;k++)
{
cout<<arr[k]<<endl;
}
return 0;
}
There were some issue with your program:
Array size should be 6 instead of 7
The for loop condition was incorrect
swap(arr[j],arr[j-i]) will break when j-i is less than 0(for instance i=1, j=0).
Program
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int arr[6]={7,8,5,2,4,6};
for(int i=0;i<5;i++)
{
for(int j=0;j<5-i;j++)
{
if(arr[j]>arr[j+1])
swap(arr[j],arr[j+1]);
}
}
for(int k=0;k<6;k++)
cout<<arr[k]<<endl;
return 0;
}
Ideone
You seem flipped for() loops over... what I got - not the most elegant solution, but I stick to the same tools you're using. Mostly. I could make it as template and it would work with any appropriate container. std::sort sometimes implemented like that.
#include <iostream>
#include <algorithm>
void bubbleSort(int arr[], int n)
{
bool swapped;
for (int i = 0; i < n-1; i++)
{
swapped = false;
for (int j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
std::swap(arr[j], arr[j+1]);
swapped = true;
}
}
// no elements were swapped, array already sorted.
if (!swapped) break;
}
}
int main()
{
int arr[] = {7,8,5,2,4,6};
bubbleSort(arr, std::size(arr));
for( auto v : arr )
std::cout << v << " ";
std::cout << std::endl;
}
In C++11 and later <algorithm> can be replaced by <utility>, it's just for swap/size.
I need to write a program that get a sentence and split its words by a delimiter(space);so I've wrote the code below but it doesn't seem to be working properly.
any idea's how to debug this code?
thanks in advance for your help.
here's what I come up with so far:
#include <iostream>
using namespace std;
const int BUFFER_SIZE=255;
int main()
{
char* buffer;
buffer=new char[255];
cout<<"enter a statement:"<<endl;
cin.getline(buffer,BUFFER_SIZE);
int q=0, numofwords=1;
while(buffer[q] != '\0'){
if(buffer[q]==' ') numofwords ++;
q ++;
}
char** wordsArray;
wordsArray= new char* [numofwords];
int lenofeachword=0, num=0;
int* sizeofwords=new int [numofwords];
for(int i=0;i<q;i++){
if(buffer[i]==' ')
{
sizeofwords[num]=lenofeachword;
wordsArray[num]=new char[lenofeachword];
num++;
}else{
lenofeachword++;
}
}
sizeofwords[num]=lenofeachword;
wordsArray[num]=new char[lenofeachword];
int k=0;
for(int i=0; i<numofwords;i++){
for(int j=0;j<sizeofwords[i];j++)
{
wordsArray[i][j]=buffer[k];
k++;
}
k++;
}
for(int i=0; i<numofwords;i++){
for(int j=0;j<sizeofwords[i];j++)
{
cout<<wordsArray[i][j];
}
cout<<endl;
}
}
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int main() {
int size, j;
char s[1005];
gets(s);
scanf("%d", &size);
j=0;
for(int i=size; i<strlen(s); i+=size) {
for(; j<i; j++) {
printf("%c", s[j]);
}
printf(" ");
}
return 0;
}
You forgot to assign zero to lenofeachword after you deretmine lenght of a word. And if int main(), you should return an int value.
I have 4 points given, I have to check if its square AND its collateral to the x and y axis.
Here is my code:
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
int wasIn(int x, int n[2])
{
for (int i=0; i<2; i++)
if (x==n[i]) return i;
return -1;
}
int main(int argc, char *argv[])
{
int x[4];
int y[4];
for (int i=0; i<4; i++)
cin>>x[i]>>y[i];
int was[2];
was[0]=-1001;
was[1]=-1001;
int countwas[2];
countwas[0]=0;
countwas[1]=0;
short old=0;
bool ok=true;
int tmp;
for (int i=0; i<4; i++)
{
if ((tmp=wasIn(x[i],was))==-1) {was[old]=x[i]; old++;} else countwas[tmp]++;
if ((tmp=wasIn(y[i],was))==-1) {was[old]=y[i]; old++;} else countwas[tmp]++;
if (old>2) { ok=false; break; }
}
if (ok && countwas[1]!=3 || countwas[0]!=3) ok=false;
//cout<<"C1: "<<countwas[0]<<endl; //debug
//cout<<"C2: "<<countwas[1]<<endl;
if (ok) cout<<"YES"; else cout<<"NO";
//system("PAUSE");
return EXIT_SUCCESS;
}
How it works:
just checks if there are 2 different numbers only, and there are exacly 4 (decrased by 1st point, so in program its 3) same occurences.
Thanx for any replies.
It sometimes crashes and gives wrong output... Maybe index out of bounds?
I won't fix your code, but the algorithm is really simple:
Check if the topleft point is on the same horizontal line as the topright point, and on the same vertical line as the bottomleft point.
Check if the bottomright point is on the same horizontal line as the bottomleft point and on the same vertical line as the topright point.
If both are true you are dealing with a collateral rectangle.
Now, if you need to know if it's a square you need to add one more check, whether the horizontal side is just as large as the vertical side.
Finally did it. I used another idea. I took the two different values x or y, generated the correct square based on that 2 numbers and compared to given. Code if someone needs:
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
int isIn(int x, int n[2])
{
for (int i=0; i<2; i++)
if (x==n[i]) return i;
return -1;
}
int cmpPoint(int x, int y, int xt[4], int yt[4])
{
for (int i=0; i<4; i++)
if (x==xt[i] && y==yt[i]) return i;
return -1;
}
int main(int argc, char *argv[])
{
int x[4];
int y[4];
for (int i=0; i<4; i++)
cin>>x[i]>>y[i];
int dif[2]={-1001,-1001};
short act=-1;
short tmp;
for (int i=0; i<4; i++)
{
if ((tmp=isIn(x[i],dif))==-1) {act++; dif[act]=x[i];}
if (act>1) break;
if ((tmp=isIn(y[i],dif))==-1) {act++; dif[act]=y[i];}
if (act>1) break;
}
if (act!=1)
{
cout<<"NO";
return 0;
}
int x2[4];
int y2[4];
bool was[4]={0,0,0,0};
x2[0]=dif[0];
y2[0]=dif[0];
x2[1]=dif[1];
y2[1]=dif[0];
x2[2]=dif[0];
y2[2]=dif[1];
x2[3]=dif[1];
y2[3]=dif[1];
bool ok=true;
for (int i=0; i<4; i++)
{
tmp=cmpPoint(x[i],y[i],x2,y2);
if (was[tmp]) {ok=false; break;}
else was[tmp]=true;
}
if (ok) cout<<"YES"; else cout<<"NO";
//system("PAUSE");
return EXIT_SUCCESS;
}