I currently have some problem with looping. Please help me to make some program that run like this :
But I have made some program that runs kind of like this :
And this is my code for my program. Maybe you can correct the wrong syntax.
#include <iostream>
using namespace std;
int main()
{
int input;
char abjad;
abjad='A';
cout<<"INPUT = ";
cin>>input;
for(int i=1;i<=input;i++){
for(int j=0;j<i;j++){
cout<<abjad;
abjad++;
}
cout<<i+1;
for(int k=0;k<input-i-1;k++){
cout<<abjad;
abjad++;
}
cout<<endl;
}
}
}
Here is your solution:
// ...
for (int i = 0; i < input; i++) {
for (int j = 0; j < i; j++) {
cout << abjad;
abjad++;
}
cout << i + 2;
// ...
I have only changed the first for loop to start with 0 instead of 1 and as a compensation to this the condition changed to i < input instead of <=. Also number output is now cout << i + 2;
This should fix your issue.
Related
I have coded this program and it works fine. I get the result I want but because we are using an old system to submit it, my code is rejected because it saying that the 3 last lines generate a blank of my code. Can someone please tell me where is the problem and how I can fix it? Thank you!
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i, row_nr;
cin >> row_nr;
if(row_nr > 1 && row_nr <= 30)
for(i = 1; i <= row_nr; i++)
{
for(int j = 0; j < row_nr; j++)
{
cout << i + j * (row_nr);
{
cout << " ";
}
}
cout << endl;
}
return 0;
}
You're outputting a space after every value, so there is going to be a space at the end of each line. You should add a check so that you don't output a space after the last value of each line. It seems like you might have intended to do this, but forgot to write the if statement.
#include <iostream>
//#include <iomanip> why?
using namespace std;
int main()
{
int row_nr;
cin >> row_nr;
if(row_nr > 1 && row_nr <= 30)
for(int i = 1; i <= row_nr; i++) //declare iterator variable in for loop statement
{
for(int j = 0; j < row_nr; j++)
{
cout << i + j * (row_nr);
if(j < row_nr - 1) //you forgot this line
{
cout << " ";
}
}
cout << '\n'; //endl flushes the buffer, unnecessary here
}
return 0;
}
I wrote a simple C++ program that finds how many duplicates are in the array.
This works perfectly for me but this is very long code. And I would like to know if there is any short code which may perform this task successfully:
#include<iostream>
using namespace std;
int main()
{
int a[10];
int reper=0,word=0,flage=0,number[10]={
0
};
//Getting Input From User
for (int i = 0; i <=9; i++)
{
cout<<"Enter The Value For "<<i<<" Index"<<endl;
cin>>a[i];
}
//Checking The Duplicates Numbers
for (int i = 0; i <= 9; i++)
{
reper=0;
flage=0;
for (int j = 0; j <=9; j++)
{
if (a[i]==a[j])
{
if (i!=j)
{
reper++;
}
}
}
number[i]=a[i];
for (int k = 0; k <=9; k++)
{
if (i!=k)
{
if(number[i]==number[k])
{
flage=1;
break;
}
}
}
//If There Are Duplicates Then Prints That Numebr, How Many Times It Repeated And Total Occurance Of That Number In The Array
if (reper!=0&&flage==0)
{
cout<<"Repeated Number Of The Array Is : "<<a[i]<<" ";
cout<<"And This Number Repeated "<<reper<<" Times "<<"And Total Occurance Of This Number is : "<<reper+1<<endl;
word=a[i];
}
}
//If There Is Nothing Any Duplicate In The Array Then Simply Prints This Message On Console
if (reper==0&&word==0)
{
cout<<"There Is Nothing Any Repeated Number Of This Array: "<<endl;
}
system("Pause");
return 0;
}
IMHO the easiest way to implement this - using http://en.cppreference.com/w/cpp/container/multiset. It has logarithmic complexity and inner methods to count repeated items.
Refer an example below:
#include <iostream>
#include <set>
int main(int argc, char *argv[])
{
std::multiset<int> ms;
//Getting Input From User
for (int i = 0; i <=9; i++)
{
std::cout<<"Enter The Value For "<<i<<" Index"<<std::endl;
int val;
std::cin>>val;
ms.insert(val);
}
bool repeated_number_found=false;
std::multiset<int>::const_iterator it = ms.begin();
while (it != ms.end()) {
int reper=ms.count(*it);
if (reper > 1){
std::cout << "Number " << *it << " repeated for " << reper << " times" << std::endl;
repeated_number_found=true;
}
it = ms.upper_bound(*it);
}
if (!repeated_number_found){
std::cout<<"There Is Nothing Any Repeated Number Of This Array"<<std::endl;
}
return 0;
}
But using this container you will loose first entrance of repeated number, if it matters to you, I will recommend using struct or std::pair to hold entrance number with entered number. In this case you will need to provide custom comparator also (refer to doc.)
I think the better way to achieve this would be to sort the array and do something like this :-
(include the header file algorithm before doing this.)
vector <int> a (10,0);
for (int i = 0; i <=9; i++)
{
cout<<"Enter The Value For "<<i<<" Index"<<endl;
cin>>a[i];
}
int count = 0;
sort(a.begin(), a.end());
for(int i = 0; i < a.size() - 1; i++) {
if (a[i] == a[i + 1]) {
count++;
}
}
cout << count << endl;
I am new to C++ I am making an array based off of user input and displaying whether the array is unique or not. my initial thought is I have to end up creating another array to store values and then compare the elements. My code compiles without errors but does not do what I want it to do. I can't used advanced methods such as hashmaps or vectors. Any help or insight is greatly appreciated!
#include <iostream>
#include <string>
using namespace std;
int main()
{
int myArray[6];
string name;
int i, k;
int ov = 0;
int newVal = 0;
for (int index = 0; index < 6; index++)
{
cout << "Enter number a number: ";
cin >> myArray[index];
}//end loop for
for (i = 0; i < 6; i++)
{
ov = myArray[i];
}
for (k = i + 1; k < 6; k++)
{
if (ov == myArray[k])
{
newVal = 1;
}
}
if (newVal == 1)
{
cout << "Not all unique";
}
else
{
cout << "All unique";
}
cin.get();
return 0;
}
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?
for example:
input:
meet me tonight
desired output:
meet
me
tonight
given output:
meet
me ton
ght
I'm really confused why the output is not what I expect. Here's what I've 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;
}
}
The problem is this snippet (comment):
if(buffer[i]==' ')
{
sizeofwords[num] = lenofeachword;
wordsArray[num] = new char[lenofeachword];
num++;
}else{
lenofeachword++; // <- this keeps increasing
}
So this snippet will skip a lot of the strings and possibly cause a seg fault somewhere along the line:
for(int i=0; i<numofwords;i++){
for(int j=0;j<sizeofwords[i];j++)
{
wordsArray[i][j]=buffer[k];
k++;
}
k++;
}
Also if this is c++, then why are you still using c-style to write this program? A simple stringstream with strings will do this in less lines of code
I'm doing an online challenge and came across one problem! I have worked out the logic on paper, but it seems my problem doesn't work. All it does is return 0 as output.
My code so far:
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int nums[50];
string res[50];
int o = 0;
for(int i=0;i<n;i++)
{
cin >> nums[i];
}
for(int i=0;i<n;i++)
{
int deliteli=1;
for(int j=1;j<=nums[i];j++)
{
if(nums[i]%j==0)
{
deliteli++;
}
}
if(deliteli == 2){
res[0] = "YES";
o++;
}
else if(deliteli != 2){
res[0] = "NO";
o++;
}
}
for(int i=0;i<o;i++)
{
cout << res[i] << endl;
}
return 0;
}
What I am doing is firstly input N number, which means how long the array is going to be and then check for each number in the array whether it's prime or not. Any ideas what am I doing wrong?
deliteli should start at 0.
deliteli should be reset at the beginning of the loop.
You use res[i] instead of res[0], otherwise you keep overwriting the first element.
j%nums[i] should be nums[i]%j, because a%b returns the remainder from dividing a by b.
' is for single characters only (C++ is perfectly happy allowing things that shouldn't be allowed to compile and run). " is for strings.
Final code:
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int nums[50];
string res[50];
int o = 0;
for(int i=0;i<n;i++)
{
cin >> nums[i];
}
for(int i=0;i<n;i++)
{
int deliteli=0;
for(int j=1;j<=nums[i];j++)
{
if(nums[i]%j==0)
{
deliteli++;
}
}
if(deliteli == 2){
res[i] = "YES";
o++;
}
else if(deliteli != 2){
res[i] = "NO";
o++;
}
}
for(int i=0;i<o;i++)
{
cout << res[i] << endl;
}
return 0;
}
Test.
for(int j=1;j>=nums[i];j++)
{
...
}
It seems like you have the loop condition wrong. It should be:
for(int j=1;j<=nums[i];j++) //Change here
{
...
}
To check if nums[i] can be divided by j you are doing j%nums[i]==0, but that needs to be nums[i]%j==0.
Your deliteli counter also has a problem.
You need to reinitialize it for each number, otherwise it will just add to it.
Also you are always setting res[0], but you would want to set res[i].
first: to check if nums[i] can be divided by j you are doing j%nums[i]==0, but that needs to be nums[i]%j==0.
second:
change for(int j=1;j>=nums[i];j++)
to for(int j=1;j<=nums[i];j++)
and last: you don't have to test number nums[i] up to nums[i] but just to square root of this so change it to sqrt(nums[i])+1. It might be slight improvement to the speed of your algorithm.