C++ : how do I make the program exactly like in the picture? - c++

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

Related

transmission a function pointer to another program file

i want to create a link between two programs throughout the execs functions .
my idea is to create function then point on it by a function pointer then send it to the other program to test it . this is my first programenter code here
1- is this possible ?
2- how ?
i get this idea because i find each time to change the function name in the main function but the remainning still as it was but if i send a pointer function as a character pointer then my programm still as it without changing
#include <iostream>
#include <cstdlib>
#include <unistd.h>
using namespace std;
void
Random(int* ,const int );
int*
selection_sort(int *arr ,const int length)
{
int i = 0,minIndex{0},tmp{0},k{0};
while(i < length-1) // T(n-1) * C1
{
minIndex = i; // Tn * C2
for(int j = i+1 ; j < length ; j++ ) // som(Ti) from i = 0 to i = length-1 )*C3.
{
if((arr)[j] < (arr)[minIndex])
minIndex = j;
}
if(minIndex != i) // Tn * C4
{
tmp = (arr)[i];
(arr)[i] = (arr)[minIndex];
(arr)[minIndex] = tmp;
}
i++;
}
return arr;
}
void
Random(int* array,const int length)
{
srand(time(nullptr));
int i{-1};
while(i++ < length)
{
array[i] = rand()%100;
sleep(0.2);
}
}
int main(int argc,char* argv[])
{
int* (*ptr)(int*,const int ) = selection_sort;
execl("/home/hellios/Documents/Algorithms/sort_Algorithms/main",(char*)ptr,0); // complete the call
return EXIT_SUCCESS;
}
sort_Algorithms/main.c
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <ctime>
using namespace std;
void
Random(int* array,const int length);
int
main(int argc,char* argv[])
{
int* (*ptr)(int *,const int ) =(int* (*) (int*,const int)) argv[1];
int arr1[100],k{0},*arr;
Random(arr1,100);
arr = (*ptr)(arr1,100);
//selection_sort(arr,100);
cout<<"out of selection_sort"<<endl;
for(int j = 0 ; j < 100 ; j++ )
{
printf("[%d]\t", arr[j]);
if(!(++k %10))
cout<<endl;
}
printf("\n");
return EXIT_SUCCESS ;
}
void
Random(int* array,const int length)
{
srand(time(nullptr));
int i {-1};
while(i++ < length)
{
array[i] = rand()%100;
sleep(0.2);
}
}

How do I make a simple program to output a pyramid of two symbols:one printed twice as the other using loops in C++?

I want:
*!!
**!!!!
***!!!!!!
// And so on.
My attempt is below:
#include <iostream>
using namespace std;
int main()
{
int a;
int ex;
for (a = 1; a <= 5; a++)
{
cout<<"*";
for(ex =1; ex<= 2*a; ex++)
{
cout<<"!";
}
cout<<endl;
}
return 0;
}
I get this as the output:
*!!
*!!!!
*!!!!!!
//and so on...
It does what I need it to do for the second symbol but I don't know how to arrange the loops so that first symbol is outputted the desired number of times and not cut off by the second loop.
there is a small logical mistake in your code, you are only printing '*' once every loop. use the code below
#include <iostream>
using namespace std;
int main()
{
int a;
int ex;
for (a = 1; a <= 5; a++)
{
cout<<std::string((a),'*');
cout<<std::string((a*2),'!');
cout<<endl;
}
return 0;
}
You need to have the cout << '*' statement in a loop as well:
int main()
{
int a;
int ex;
for (a = 1; a <= 5; a++) // signifies the number of lines to print
{
auto i = 1;
while (i <= a) // prints * a times
{
cout<<"*";
++i;
}
for(ex =1; ex<= 2*a; ex++) // prints ! 2*a times
{
cout<<"!";
}
cout<<endl;
}
return 0;
}
You need another loop to print a-counted * symbols inside the main loop.
#include <iostream>
using namespace std;
int main()
{
int a;
int ex;
for (a = 1; a <= 5; a++)
{
for(int i = 0; i < a; ++i)
{
cout<<"*";
}
for(ex =1; ex<= 2*a; ex++)
{
cout<<"!";
}
cout<<endl;
}
return 0;
}
Another solution is:
#include <iostream>
using namespace std;
int main(){
int times = 5;
char simbol1 = '*', simbol2 = '!';
for(int i=1 ; i<=times ; i++){
for(int k=0; k<i; k++) cout << simbol1;
for(int j=0; j<i*2; j++) cout << simbol2;
cout << endl;
}
return 0;
}

Array compression and search in C++(basics)

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

How to debug this code for splitting an array by space?

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.

Problems with writing powerset code

I'm trying to generate the powerset of a set and I wrote this code. The problem is, when user enter two similar member of the set it dosen't work properly. What can I do?
Here is my code:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
char obtain(char *p,int n)
{
int i;
for(i=0;i<n;i++)
{
cout<<"enter member"<<(i+1)<<"\n";
cin>>*(p+i);
}
return *p;
}
void set_display(char *p,int n)
{
cout<<"{";
for(int i=0;i<n;i++)
{
cout<<*(p+i)<<",";
}
cout<<"}";
}
void powset(char *p,int n)
{
unsigned int m = (double)pow((double)2, n);
int i, j;
for(i = 0; i < m; i++)
{
cout<<"{";
for(j = 0; j < n; j++)
{
if(i& (1<<j))
cout<<*(p+j);
}
cout<<"}\n";
}
}
Ok I could not debug your code where you are missing. but since you posted your question I written an code in pure C. May be you find it helpful.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main(int argc, char* argv[]){
int N;
char y[20]={0};
int i,j;
y[0] = 'a';
y[1] = 'b';
y[2] = 'c';
N = 3;
int powerSet;
powerSet=pow(2,N);
for(i = 0; i<(powerSet);i++){
for(j=0;j<N;j++){
if((i & (1<<j))){
printf("%c ",y[j]);
}
}
printf("\n");
}
printf("\n");
return EXIT_SUCCESS;
}
And its working:
:~$ gcc x.c -lm -Wall
:~$ ./a.out
a
b
a b
c
a c
b c
a b c
[ANSWER]
You error case: When two symbols are same.
y[0] = 'a';
y[1] = 'a';
y[2] = 'c';
:~$ ./a.out
a
a
a a
c
a c
a c
a a c
But this is wrong according to set theory. Because in Set we can't have same element twice (more then onec). BUT ALSO INPUT IS WRONG: (a, a, c) is not a set. So code is usable.