#include<iostream>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
using namespace std;
int swapper(int n[],int s
)
{
for(int i=0;i<s;i++)
{
int temp=n[i];
n[i]=n[i+1];
n[i+1]=temp;
}
cout<<"\n Array Swapped !!!";
for (int i=0;1<s;i++)
{
cout<<n[i]<<"\t";
}
return 0;
}
int main()
{
int n[20]; int s;
cout<<"\n enter array size:";
cin>>s;
cout<<"enter no in array according to size given";
for(int j=0;j<s;j++)
{
cin>>n[j];
}
swapper(n,s);
return 0;
getch();
}
the output to this program does not swap the array elements,
instead it starts producing numbers in plenty
the entire code is written here
all other suggested changes have been made
the function is supposed to take in an integer array and its size as parameters and display an array with its adjacent elements swapped.
The call
swapper(n[],s);
is not right. Assuming the first argument of swapper is a int*, it needs to be:
swapper(n,s);
Remove the [] in your swapper function.
Related
I am trying to pass a dynamic memory allocated array and its size to a function 'sum' but it is giving error of permissive what should I do?
#include<conio.h>
#include<iostream>
using namespace std;
int sum(int n[], int *m)
{
for(int z=0;z<*m;z++)
{
cout<<"\n the output is = "<<n[z]<<"\n";
}
}
int main()
{
int *n,*m,a; //declaration is done here**strong text**
cout<<"enter the size of array = ";
m=new int;
cin>>*m;
n=new int[*m];
for(int i=0;i<*m;i++)
{
cout<<"\n enter the "<<i+1<<" array = ";
cin>>n[i];
cout<<"\n";
}
/* for(int z=0;z<*m;z++)
{
cout<<"\n the output is = "<<n[z]<<"\n";
}*/
int sum(n,&m);//here "m" is an pointer and I am trying to pass int in a function with an array
return 0;
}
Your code should, probably, look like the following (Linux Ubuntu + gcc):
#include <iostream>
using namespace std;
int sum(int n[], int m)
{
int s=0;
for(int z=0; z<m; z++)
{
cout<<"\n array["<<z<<"]= "<<n[z]<<"\n";
s+=n[z];
}
return s;
}
int main()
{
int *n,m;
cout<<"enter the size of array = ";
cin>>m;
n=new int[m];
for(int i=0; i<m; i++)
{
cout<<"\n enter array["<<i+1<<"] value = ";
cin>>n[i];
cout<<"\n";
}
int s = sum(n, m);
cout<<"s="<<s<<endl;
return 0;
}
There is no use allocating the size of the array m dynamically. It is an ordinary int variable and can be initialized as
cin>>m;
You may also write the sum prototype in the form
int sum(int * n, int m)
It is another way of passing a 1-dimensional array as a function parameter.
Speaking frankly, these questions are the very basics of the language.
You should, probably, read something like
Dynamic memory allocation/dynamic arrays
about dynamic memory allocation and dynamic arrays and
Simple cases of std::cin usage
about the simplest cases of std::cin usage in C++.
The function should return 156 with a "Z" input but it outputs 0.
If the letter's U-Z, the function finds the largest factor of the letters numeric value other than the value itself and multiplies it by 12.
Tried putting cout in the function and it works somehow. Don't know where's the problem. Someone help.
#include<iostream>
#include<string>
#include<ctype.h>
using namespace std;
int uz(int n);
int main(){
string letter, alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int alphSize=alphabet.size(), l=0, total=0;
cin>>letter;
int letterSize=letter.size();
for(int i=0;i<letterSize;i++){
for(int j=0;j<alphSize;j++){
if(toupper(letter[i])==alphabet[j])
l=j+1;
}
if(l>20&&l<=26){
l=uz(l);}
total+=l;
}
cout<<total<<endl;
return 0;
}
int uz(int n){
int size=1;
int arr[size];
for(int i=1;i<=n;i++){
if(n%i==0){
size++;
arr[size]+=i;
}
}
n=12*arr[size-1];
//cout<<n;
return n;
}
#include<iostream>
using namespace std;
int main(){
int n,i=0,j=1;
cin>>n;
int m;
while(m!=n){
m=i*j;
cout<<m;
i++;
j++;
}
return 0;
}
I want to display Pronic Number ie . 0,2,6,12,20,30,42... When I insert value of n as Pronic Number Code run fine ... and desire output is given.. Bug is when you insert value of n=15 while loop goes to infinity but i want to display till 15 or less than
15.. Here 15 is not a Pronic Number..
Initialize m before using it, plus change your condition to stop the loop when m>n
#include<iostream>
using namespace std;
int main(){
int n,i=0,j=1;
cin>>n;
int m=0;
while(m<=n){
cout<<m<<" ";
i++;
j++;
m=i*j;
}
return 0;
}
# include <iostream>
# include <cmath>
using namespace std;
int main()
{
int p;
int n;
int q;
cin>>n;
int r;
r=0;
for (int i=0,n; n>1; i=i+1,n=n/2)
{
p=n%2;
q= p*(pow(10,i));
r=r + q;
}
cout<<r;
system("pause");
return 0;
}
I am not supposed to use arrays. It compiles fine but when executed and a number is entered, it doesn't produce the desired results.
For instance, when 22 is entered, it gives -2147483648 whereas the desired output would be 10110.
your way is limited and not effient in converting to binary
you should use string it's more helpful and the range is big enough for any number
this is my code for decimal-to-binary
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
long long n;
string s,bin;
stack<string> res;
cin>>n;
while(n>0)
{
if(n%2==0)
s='0';
else
s='1';
res.push(s);
n=n/2;
}
while(!res.empty())
{
bin=bin+res.top();
res.pop();
}
cout<<bin<<endl;
return 0;
}
I hope it will help you.
int i=0,n;
should be
int i=0;
I don't know what you thought you were doing there, but what you are actually doing is declaring another variable n. Because the second n variable doesn't have a value the rest of the code doesn't work.
That's not the only problem with your code, but I'm sure you can figure out the rest.
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int bins(int* p,int lo,int hi,int t)
{//BinarySearch
int mid=0;
int c=0;
if(lo<hi)
{
mid=(lo+hi)/2;
if(p[mid]==t) c=1;
else if(t>p[mid])
bins(p,mid+1,hi,t);
else if(t<p[mid])
bins(p,0,mid,t);
}
return c;
}
int main()
{
clrscr();
int target;
int k;
int count=0;
cout<<"Enter the number of elements:";
cin>>k;
int* numbers=new int(k);
cout<<"Enter the target element"<<endl;
cin>>target;
cout<<"Enter the elements:"<<endl;
for(int i=0;i<k;i++)
cin>>numbers[i];
int* nu=new int(k);
memset(nu,0,sizeof(int));
/*for(i=0;i<k;i++){
cout<<numbers[i]<<endl;
}*/
for(i=0;i<k;i++)
{
int bool=bins(nu,0,k,numbers[i]);
//printf("%d\n",bool);
if(bool)
{
count++;
}
else
{
int tg=target-numbers[i];
cout<<"targetval:"<<tg<<endl;
nu[i]=tg;
}
//bool?count++:(nu[i]=target-numbers[i]);
}
cout<<"Count is:"<<count<<endl;
getch();
return 0;
}
this is the code i have written for TwoSum problem in leetcode,instead of using HashMap whose time complexity is O(n) i used binary search whose complexity is O(logn)
The problem i'm facing is the input i give to the array numbers[i] takes only 3 values and from then it stores junk values even after assigining proper inputs from console
this program has been compiled succesfully on Turbocompiler on system
int* numbers=new int[k];////allocates an array of k adjacent integers. (undefined values)
You are allocating for an array. Check the notations.
Also you are using a keyword as a variable.(bool).
What have you done?
int *numbers=new int(k);////allocates an integer, set to k. (same syntax as constructors)
Leetcode doesn't give exact code. It will have syntax errors or compilation erros. You have to fix them to make it work. In this case, following are the issues,
int* numbers=new int[k]; //not int* numbers=new int(k);
Another thing is that bool is a keyword, that is used to give true or false for variables. You have to use another variable.