*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;
}
If I write as following, Segmentation fault occurs. But if I write printf(messages[0]) or printf(messages[1]), I don't have error. WHY? I wanted to print all members of array using "for".
void givetag(char *array[]); //define function
int main()
{
char* messages[3];
givetag(messages); //sub function
int i;
for(i=0;i<3;i++)
{
printf(messages[i]);
}
}
void givetag(char *array[])
{
int i;
for(i=0; i<3; i++)
{
array[i]= (char*) malloc(10);
scanf("%s", array[i]);
}
}
#include <stdio.h>
#include <stdlib.h>
void
givetag(char *array[])
{
int i;
for(i=0; i<3; i++){
array[i]= (char*) malloc(100);
scanf("%s", array[i]);
}
}
int main()
{
char* messages[3];
givetag(messages); //sub function
int i;
for(i=0;i<3;i++) {
printf(messages[i]);
}
printf("\n");
return 0;
}
as you see in picture of out put, you must add malloc size (i change code).
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<<' ';
}
I know the logic how to merge two arrays but the problem is how to code.
This was my code n it is giving correct ans but my sir told me that do it again,please tell me what I have to add in this code,
#include<iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2);
int main()
{
const int size1=8;
const int size=12;
int arrayA[size1]={10,25,37,49,50,51,55,60};
int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
mergeArrays(arrayA,size1,arrayB,size);
}
int mergeArrays(int array1[],int size1,int array2[],int size2)
{
int size3=size1+size2;
int *array3=new int[size3];
int k=0;
for(int i=0;i<size1;i++)
{
array3[k]=array1[i];
cout<<" "<<array3[k];
}
int j=0;
for(int i=size1;i<size2;i++)
{
array3[k]=array2[j];
}
for(int i=size1;i<size2;i++)
{
for(int j=0;j<size2;j++)
{
array3[i]=array2[j];
cout<<" "<<array3[i];
}
cout<<endl;
delete[]array3;
return array3[k++];
}
}
I had searched this in many places but could not corrected my code
I had written this code but it is not giving correct ans.
#include<iostream>
using namespace std;
int merge(int *a,int *b,int aSize,int bSize);
int main()
{
const int aSize={8};
const int bSize={12};
int arrayA[aSize]={10,25,37,49,50,51,55,60};
int arrayB[bSize]={2,5,26,27,29,32,40,45,70,80,90,95};
merge(arrayA,arrayB,aSize,bSize);
return 0;
}
int merge(int *a,int *b,int aSize ,int bSize)
{
int cSize=aSize+bSize;
int *c=new int[cSize];
int j=0,k=0;
int i=0;
while(i<=aSize&&j<=bSize )
{
if(a[aSize ]<=b[bSize])
{
c[k]=a[aSize];
k++;
i++;
}
else
{
c[k]=b[bSize];
k++;
j++;
}
}
for(int i=0;i<k;i++)
{
cout<<c[i]<<endl;
}
delete[]c;
return c[k++];
}
your sir request you do Merging two arrays in ascending order. so i think you should return a new array, fill with array1 and array2's element, and the elements should be ascending order. here is a implement.(suppose your input arraies is already in ascending order.)
#include <iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2, int outArray[]);
int main()
{
const int size1=8;
const int size=12;
int arrayA[size1]={10,25,37,49,50,51,55,60};
int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
int outArray[size1+size];
int len = mergeArrays(arrayA,size1,arrayB,size, outArray);
cout <<" "<< len;
for (int i = 0; i< size1+size; ++i){
cout <<" " << outArray[i];
}
}
int mergeArrays(int array1[], int size1, int array2[], int size2, int outArray[])
{
int i=0, j=0, k=0;
int retSize = size1+size2;
while (k<retSize){
if (i==size1){// only left array2, copy it
for (; j<size2; ++j){
outArray[k++] = array2[j];
}
}else if (j == size2) { // only left array1, copy it
for (; i<size1; ++i){
outArray[k++] = array1[i];
}
}
else if (array1[i] > array2[j]){ // copy the min value to outArray
outArray[k++] = array2[j++];
}else{
outArray[k++] = array1[i++];
}
}
return k;
}
now, let's look at your first code:
int mergeArrays(int array1[],int size1,int array2[],int size2)
{
int size3=size1+size2;
int *array3=new int[size3];
int k=0;
for(int i=0;i<size1;i++)
{
array3[k]=array1[i]; // k is not changed, so you just assign array1's each value to array3[0]
cout<<" "<<array3[k];
}
int j=0;
// what's the purpose of this loop?
// and in loop, you don't use i, you just repeat set array3[0] = array2[0]!!
for(int i=size1;i<size2;i++)
{
array3[k]=array2[j];
}
for(int i=size1;i<size2;i++) // if array2's length bigger than array1's, will enter this loop.
{
for(int j=0;j<size2;j++)
{
array3[i]=array2[j]; // this just repeat assign array2's each value to array3[i]!!
cout<<" "<<array3[i];
}
cout<<endl;
delete[]array3;
return array3[k++]; // you delete array3, but at here you access it!! this will crash!
// also, in this for i loop, you have return, so it will only execute once.
}
// reach function end and no return if not enter for loop.
}
I haven't looked at your second code. I think you still need to do more study.
Well my program is working with huge digits and I should wait to much for example calculating 20! takes too much time ( calculating all permutation).
How can I output first 20 results not all 20? I tried to capture this while the program was running in icon, but I can't.
Here is my code
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <vector>
#include <fstream>
using namespace std;
int X[100];
LARGE_INTEGER start, finish, freq;
int N;
int count_number;
vector<int> numb;
vector<int> cycle;
bool search (vector<int> source, int data)
{
for (int i=0; i<source.size(); i++)
if (source[i] == data)
return true;
return false;
}
void fill(int find)
{
find++;
int data = X[find-1];
int temp = find;
while (data != find){
temp = data;
data = X[temp-1];
numb.push_back(temp);
cycle.push_back(temp);
}
if (data == find) {
numb.push_back(find);
cycle.push_back(find);
}
}
void Swap(int a,int b)
{
int t=X[a];
X[a]=X[b];
X[b]=t;
}
void Generate(int k)
{
if (k==N)
{
for(int i=0;i<N;i++)
cout<<X[i]<<" ";
cout<<"Циклы: ";
for (int i=0; i<N; i++) {
if (!search(numb, i+1)) {
fill(i);
cout<<'(';
for (int i=0; i<cycle.size(); i++) {
cout<<cycle[i];
if(i != cycle.size()-1)
cout<<',';
}
cout<<") ";
cycle.clear();
}
}
numb.clear();
cout<<"\n";
count_number++;
}
else
{
for(int j=k;j<N;j++)
{
Swap(k,j);
Generate(k+1);
Swap(k,j);
}
}
}
int main()
{
setlocale(LC_CTYPE,"English");
cout<<"N=";
cin>>N;
QueryPerformanceFrequency( &freq );
QueryPerformanceCounter( &start );
for(int i=0;i<N;i++)
X[i]=i+1;
Generate(0);
cout<<endl;
cout<<endl;
QueryPerformanceCounter( &finish );
double time = (finish.QuadPart - start.QuadPart) / (double)freq.QuadPart;
cout<<"Algorith time:"<<time<<" \t seconds"<<endl;
cout<<"All permutation:"<<count_number<<endl;
FILE myfile;
cin.get();
getch();
return 0;
}
included
created FILE mmm.
how to make?