Repetition Structure Help

Hi, I'm in my first semester of C++, and I'm having trouble with a program.
I need to write a program that reads numbers one by one from a text file, check whether a number is well-ordered or not, and print the number with “well-ordered” if the number is well-ordered, otherwise print the number with “not well-ordered

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  #include<iostream>
#include<cmath>
using namespace std;

bool desirable(int num, int N)
{
    int digits[N];
    for (int i=0; i<N; i++)
    {
        digits[i]=num%10;
        num/=10;
    }
    for (int i=0;i<N-1;i++)
    {
        if (digits[i]>digits[i+1])
            continue;
        else
            return false;
    }
    return true;
}

int main()
{
    printf("Enter: ");
    int N;
    scanf("%d",&N);
    int low = (int)pow(10.0, double(N-1));
    int high = (int)pow(10.0, double(N)) -1;

    for (int i=low; i<=high; i++)
    {
        if(desirable(i,N))
            printf("%d\n",i);
    }
    return 0;
}

This is what I have so far. Any help is appreciated.
What exactly do you mean that a number is well ordered? It's digits are ordered, you're receiving them in a way that they're already ordered? I just find it a slightly confusing use of language as any set of numbers is "well ordered" according to the well ordering principle.
Last edited on
I'm having trouble with a program.

What trouble?


Note 1:
Line 7 depends on "variable length array" feature that is not standard C++. You don't actually need an array; two ints would be enough.

You could write line 7 as: std::vector<int> digits( N );


Note 2:
You do include <iostream>, but you don't use any of its features.
You do use I/O-functions from C's library, but don't include their header.
By well ordered I mean increasing from left to right, like 368 as opposed to 287.
I've replaced with line 7 with what you suggested, kesk.
Any ideas on what features of <iostream> to use?
Like I said, I'm new to this.

As for <iostream>, you're going to want to use <fstream> for file input but iostream will work fine for testing.

std::ifstream in("Filename.txt")

and then use in as if it were cin.

May I ask the purpose of int low and int high in main?
Last edited on
....You know, I don't remember.
<code> #include<iostream>
#include<cmath>
using namespace std;

bool desirable(int num, int N)
{
std::vector<int> digits( N );
for (int i=0; i<N; i++)
{
digits[i]=num%10;
num/=10;
}
for (int i=0;i<N-1;i++)
{
if (digits[i]>digits[i+1])
continue;
else
return false;
}
return true;
}

int main()
{
std::ifstream in("Filename.txt")
printf("Enter: ");
int N;
scanf("%d",&N);

{
if(desirable(i,N))
printf("%d\n",i);
}
return 0;
} </code>
Is this looking better?
http://www.cplusplus.com/doc/tutorial/basic_io/
http://www.cplusplus.com/doc/tutorial/files/

Is this looking better?

Code tags should have brackets.

Your current program prints the number only if it is "desirable". Your instructions want a printout for every number and merely different printed text based on "desirability".


You (must) have a compiler. When you do compile a program, the compiler can show warning and error messages. Those messages state the line of code that has an issue. Learn to look at those messages. The first error is usually the most meaningful.

If you see an error that you cannot fix or understand, show it to us with the code.
Alright, this version looks a lot better than my old one.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{ 
ifstream infile("numbers.txt"); 

int n,m; 
infile>>n; 
for(int i=0;i<n;i++) 
{ 
infile>>m; 

int j=0,dig,a,flag=1; 
while(m) 
{ 
dig=m%10; 
m=m/10; 
if(j++==0){a=dig;continue;} 
else if(a<dig) {flag=0;break;} 
else a=dig; 
} 
if(flag==1)cout<<"Well-ordered"<<endl; 
else cout<<"Non well-ordered"<<endl; 

Are there any specific things I shoud add?
Topic archived. No new replies allowed.