Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

What did I do wrong in this program?


  • Please log in to reply
4 replies to this topic

#1 antoniu200

antoniu200

  • Members
  • 207 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:49 PM

Posted 03 March 2016 - 09:49 AM

The request is pretty long:

Consider a sequence x1, x2, ..., xn consists of n distinct positive integers. A maximum sequence number

neighboring elements in string form xi xi 1, ..., x k-1, XK, XK 1, ..., x j (1≤i <k <j≤n) property that xi <xi 1 <. .. <xK-1 <xK> xK 1> ...> XJ, xK is called mountain top. Two mountain sequences have a common element in a row maximum. A mountain sequence has at least 3 elements. An example of a format string values ​​3 4 6 8 does not contain any sequence mountain, and one made with values ​​3 4 8 1 2 5 0 2 contains sequences mountain: 3 August 4 1 and 1 2 5 0.

After determining all sequences mountain peaks and their sequences are deleted from the mountain tops string and continue the procedure repeated with new determination sequences mountain peaks and their new string obtained. The procedure stops when the string is no longer any sequence administration.

Requirement
Write a program that reads numbers n, x1, x2, ..., xn and then determine:
a) the initial string sequences mountain;

b)the total number of mountain sequences obtained from the initial string to one that does not contain any sequence mountain;
c) number of items in the final string does not contain sequences mountain.

Entry data

Munte.in input file contains the first line number n, and the next line of natural numbers x1, x2, ..., xn separated two by two by one space.

Output data

Munte.out output file will contain the first line of a natural number as required) on the second line a natural number as required  on the third line a natural number as required c).

Restrictions and specifications
3 ≤ n ≤ 100

0 ≤ xi ≤ 100000, 1 ≤ i ≤ n

Requirement for correct solution a) obtain 20% of the score. Requirement for correct solution B) obtain 40% of the score.

Requirement for fair c) obtain 40% of the score.

 

 

 

 

I hope you understand something, I translated it with Google,as I didn't have the time to translate it myself.

 

The program is created with Codeblocks, in C++.

 

 

#include <iostream>
#include <fstream>

using namespace std;
int v[50],n,i,nr,j,k,a[50],b,nr1,nr_sf;
int main()
{
    fstream out("munte.out");
    cout<<"Write the number of numbers that you will give! ";
    cin>>n;
    for (i=1;i<=n;i++)
    {
        cout<<"Number "<<i<<"=";
        cin>>v[i];
    }
    for (i=2;i<n;i++)
    {
        j=i-1;
        k=i+1;
        if((v[i]>v[j]) and (v[i]>v[k]))
        {
            nr=nr+1;
            a[i]=v[i];
        }
    }
    nr_sf=nr_sf+nr;
    out<<nr<<endl;
    nr1=1;
    do
    {
    for (i=2;i<n;i++)
    {
        v[i]=v[i]-a[i];
    }
    for (i=2;i<n;i++)
        if(v[i]!=0)
        {
            cout<<v[i]<<" ";
        }
    cout<<"Write the number of numbers that were shown! ";
    cin>>b;
    cout<<"Write the numbers that were shown in the same order! ";
    for (i=1;i<=b;i++)
    {
        cin>>v[i];
    }
    for (i=2;i<=b;i++)
    {
        nr1=0;
        j=i-1;
        k=i+1;
        if((v[i]>v[j]) and (v[i]>v[k]))
        {
            nr1=nr1+1;
            a[i]=v[i];
        }
    }
    if(nr1==0)
    {
        out<<nr;
    }
    if(nr1!=0)
    {
        out<<nr1<<endl;
    }
    nr_sf=nr_sf+nr1;
    }
    while (nr1!=0);
    out<<nr_sf;
    return 0;
}

 

 

Sorry I couldn't attach the files, but I didn't know how.


Edited by antoniu200, 03 March 2016 - 09:50 AM.


BC AdBot (Login to Remove)

 


#2 KingDavidlll

KingDavidlll

  • Members
  • 297 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:04:49 AM

Posted 04 March 2016 - 02:23 AM

Straight off the bat, your input is read in via input when the task says that it should be read in by a file.  

 

For me to go into detail you'll have to correct the second paragraph as a lot of things are culled by google translate when putting numbers into it, and go through it again and make sure it makes sense and is correct as some of the other parts are strange as well.

 

I would also highly recommend just checking if this works yourself as half the job of being a programmer is to be able to write code which works, and if it doesn't you should have at least a vague idea of why it isn't or know whether it is or not.  Try writing some sample data in a text file which you place sequences of numbers which should be detected as a mountain sequence and see if it picks it up.



#3 antoniu200

antoniu200
  • Topic Starter

  • Members
  • 207 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:49 PM

Posted 04 March 2016 - 06:46 AM

It picks them up. I will take care of the translation when I have time, which will be soon, I hope. It picks them up. I didn't read the values from a file because it wouldn't detect "in" as a command in fstream, it only detects "out" as one.

#4 KingDavidlll

KingDavidlll

  • Members
  • 297 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:04:49 AM

Posted 04 March 2016 - 10:47 PM

Ahhh, I just had a look at your fstream out.  Just letting you know that when you do fstream out(...) that means the variable is called out, not that it is special in that it is reading in or writing out. 

 

I would highly recommend to look at the example here:

http://www.cplusplus.com/reference/fstream/fstream/fstream/

and 

http://www.cplusplus.com/reference/fstream/fstream/open/

 

If you still can't get it working let me know.



#5 antoniu200

antoniu200
  • Topic Starter

  • Members
  • 207 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:09:49 PM

Posted 07 March 2016 - 09:12 AM

But wouldn't this "std::fstream fs ("test.txt", std::fstream::in | std::fstream::out);" mark the file for in AND for out too? If no, I don't understand.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users