Problems with writing powerset code - c++

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.

Related

My code shows 4(Coreect) in standard input, 5645 in USACO grader, and 1862013874 with scanf/printf. What is the problem?

Here is the code in C++ with scanf/printf.
I ran it with file I/O but cin/cout on my laptop as well as command-line cin/cout. Both worked fine. The USACO grader did not work with these two either. scanf/printf does not work either. (please help, i know this will probably be a dumb mistake).
#include <cstdio>
#include <cassert>
#include <algorithm>
using namespace std;
using ll = long long;
int main(){
int n, k;
scanf("%d", &n);
scanf("%d", &k);
char s[10];
int gestures[n+1], dp[n+1][k+1][3];
for (int i = 1; i <= n; i++){
scanf("%s", s);
if (s[0]=='H') gestures[i] = 0;
else if (s[0]=='P') gestures[i] = 1;
else gestures[i] = 2;
}
int res = 0;
for (int i = 1; i <= n; i++){
for (int s = 0; s <= k; s++){
for (int p1 = 0; p1 < 3; p1++){
if (n){
dp[i][s][p1] = max(dp[i-1][s-1][(p1+1)%3], dp[i][s][p1]);
dp[i][s][p1] = max(dp[i-1][s-1][(p1+2)%3], dp[i][s][p1]);
}
dp[i][s][p1] = max(dp[i-1][s][p1], dp[i][s][p1]);
dp[i][s][p1] += (p1==gestures[i]);
if (i==n) res = max(res, dp[i][s][p1]);
}
}
}
printf("%d\n", res);
}

My program is exiting with return value 3221225620

I have written a cpp code for array rotation and the file handling part is a bit tricky for me.
The code itself is correct but even though the files are in the same directory as the code it is not working for some reason
#include <math.h>
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
using namespace std;
int ar[100];
// #define crap ios_base::sync_with_stdio(false);cin.tie(NULL);
int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
void leftRotate(int arr[], int d, int n)
{
d = d % n;
int g_c_d = gcd(d, n);
for (int i = 0; i < g_c_d; i++) {
int temp = arr[i];
int j = i;
while (1) {
int k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}
//int *func(int m)
//{
// int *p;
// p=new int[m];
// return (p);
//}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
// crap;
int n,d;
cin>>n>>d;
// ar=func(n);
// cout<<sizeof(ar);
for (int i=0;i<n;i++)
{
cin>>ar[i];
}
leftRotate(ar, d, n);
for (int i = 0; i < n; i++)
cout << ar[i] << " ";
return 0;
}
The code itself is working and returning correct output in the terminal but I cant seem to find the issue here
I have tried without file handling and it was giving a return value 3221225620 at the terminal
sample input is:
5 2
1 2 3 4 5
here shows a possible error:
3221225620 (0xC0000094): Zero Division Error
means that a divisor in your code could sometime be zero.
as for your code(line 20: d = d % n;), when your n is 0, the output will show return value 3221225620
so please check your data in "input.txt"

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

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

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

bubble sorting and getchar in c

I am working with microsoft visual studio 2012 and trying to make a bubble sort. Here is my code:
#include "stdafx.h"
#include "String.h"
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int array[100], n, c, d, swap;
printf("enter numbers of elements\n");
scanf_s("%d",&n);
printf("enter %d integers\n", n);
for (c = 0; c < n; c++){
scanf_s("%d", array);
}
for (c = 0; c < (n - 1); c++)
{
for (d = 0; d < n - c - 1; d++)
{
if (array[d]>array[d + 1]){
swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
}
}
}
printf("sorted list in ascending order:\n");
for (c = 0; c < n; c++){
printf("%d\n", &array[c]);
}
getchar();
return 0;
}
First of all I can't make console stay for a key entry. getchar() seems like not working but i don't have any error. Plus when I see console for a second, I can say that numbers are listed like "-310892". I don't know why.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main(void)
{
int array[100],n,c,d,flag,swap;
printf("Enter the no. of elements\n");
scanf("%d",&n);
for(c=0;c<n;c++)
{
scanf("%d",&array[c]); // here you have to add & for assigning a address to variable in memory
}
for(c=0;c<(n-1);c++)
{
flag=0;
for(d=0;d<n-c-1;d++)
{
if(array[d]>array[d+1])
{
swap=array[d];
array[d]=array[d+1];
array[d+1]=swap;
flag=1;
}
}
if(flag==0)
break;
}
printf("sorted elements in ascending order:\n");
for(c=0;c<n;c++)
{
printf("%d\t",array[c]);// you want to print the element not its address so no need of &
}
getch();
return 0;}
Please note i'm adding additional variable "flag" which helps to increase the efficiency of your program because the loop will break when your elements are done sorting but in your program the loop might be doing some extra iteration.
I fixed the typos that others mentioned and added system("pause"). Worked fine for me on VS 2010. Haven't got access to VS 2012 to test it. Here's your code:
#include <string.h>
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("enter numbers of elements\n");
scanf("%d",&n);
printf("enter %d integers\n", n);
for (c = 0; c < n; c++){
scanf("%d", &array[c]);
}
for (c = 0; c < (n - 1); c++)
{
for (d = 0; d < n - c - 1; d++)
{
if (array[d]>array[d + 1]){
swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
}
}
}
printf("sorted list in ascending order:\n");
for (c = 0; c < n; c++){
printf("%d\n", array[c]);
}
system("pause"); // <---- Added this!!!
return 0;
}
Hope it works fine for you too.
Concerning your bubble sort implementation:
your scanf_s in the first for loop always reads the number into the first position of the array.
your printf in the last for loop expects an integer but you supply an address.
To prevent the console from disappearing, you could replace getchar() by system("pause"), although this is not portable.
Correcting these things, the bubble sort works for me:
#include "stdafx.h"
#include "String.h"
#include <iostream>
using namespace std;
#include <string.h>
int main() {
int array[100], n, c, d, swap;
printf("enter numbers of elements\n");
scanf_s("%d",&n);
printf("enter %d integers\n", n);
for (c = 0; c < n; c++) {
scanf_s("%d", &array[c]);
}
for (c = 0; c < (n - 1); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d + 1]) {
swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
}
}
}
printf("sorted list in ascending order:\n");
for (c = 0; c < n; c++){
printf("%d\n", array[c]);
}
system("pause");
return 0;
}