C++ code for school assignment

This is a discussion on C++ code for school assignment within the C++ Programming forums, part of the General Programming Boards category; hello all My problems Before 2 weeks my semester started and we got the programming tasks to solve but i ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Croatia
    Posts
    2

    Unhappy C++ code for school assignment

    hello all

    My problems
    Before 2 weeks my semester started and we got the programming tasks to solve but i newer had before
    programming lesson in school and cant solve this tasks.
    I asked a few friends to help me but they do not know or will not want to help me.


    First task:
    Code:
    Write C + + program that will load a string from the keyboard or the number can be set at the beginning of the program. 
    The program should print screen in which is shown if the string entered is in or not in supported data type INT 
    (eg, the class of characters which is composed of integers, eg, -3, +5.7 are trademarks that 
    belong to the category of INT).
    Second task:
    Code:
    Write C + + program that will load a string from the keyboard or the number can be set at the beginning of the program. 
    String consists entirely of parentheses - (), [,], {i}. Program to print whether the series od brackets is good or not!
    A series of brackets is correct if:
    1. Has the same number of open and closed parentheses of the same type.
    2. There is no overlap (first appeared open, then its corresponding closed parenthesis) Otherwise, a number of brackets is not correct.
    (Example: (()[({})()]) is correct, (((([}} is not correct because there is no equal number of open and closed brackets, (([)]) is not correct 
    because there is overlap of brackets and parentheses.
    Thanks for any help, any part of the code if it is good is help to me.

    sorry for my bad english
    Last edited by Pupo; 03-09-2010 at 05:44 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You might want to check the forum rules. You are supposed to post what you have tried (code), not your question - in a code box that makes it unreadable anyway! (Edit your post)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Croatia
    Posts
    2
    Quote Originally Posted by anon View Post
    You might want to check the forum rules. You are supposed to post what you have tried (code), not your question - in a code box that makes it unreadable anyway! (Edit your post)
    Ok, sorry for my inattention!

    In first task i manage to load characters

    Code:
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int main()
    {
    int i,N;
    char X[11];                          
    cout<<"Set field size! (max N = 10) "; 
    cin >>N;
    for (i=0;i<N;i++)               
    {
    cin>>X[i];                       
    }
    {
    if (X[0]<=2147483647 and X[0]=>-2147483648)   /*Dont know how to declare INT variable. I try with simple int, but an error has appeared :( */
    {
    cout<< "The first character in the lexical class of INT! ";
    }
    else
    {
    cout<<"The first character is not in the lexical class of INT! ";  /* This section is for first character in field */
    }
    }                                 
    
    system ("PAUSE");
    return 0;
    }
    Last edited by Pupo; 03-09-2010 at 07:49 AM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    People will be more receptive if you properly indent your code. If you are going to continue to program for more than a few weeks, you *will* be using indentation, so you might as well start now.
    Code:
    int main()
    {
    	int i,N;
    	char X[11];                          
    	cout<<"Set field size! (max N = 10) "; 
    	cin >>N;
    	for (i=0;i<N;i++)               
    	{
    		cin>>X[i];                       
    	}
    	{
    	if (X[0]<=2147483647 and X[0]=>-2147483648)   /*Dont know how to declare INT variable. 
                                            I try with simple int, but an error has appeared :( */
    	{
    		cout<< "The first character in the lexical class of INT! ";
    	}
    	else
    	{
    		cout<<"The first character is not in the lexical class of INT! ";  
                /* This section is for first character in field */
    	}
    	}                                 
    
    	system ("PAUSE");
    	return 0;
    }
    Notice this more clearly reveals the spurious braces in the code. Also, I broke the comments to shorten the lines slightly. As you can perhaps see, using run on lines in a code block has caused the forum thread to be irritatingly widened.

    Your input check will not work. X[0] is one single byte, eight binary bits:

    0101 1010

    This can represent a range from 0-255 (or in this case -128 to 127, since it is a signed type) -- it CANNOT be like 22147483647, so there is no point in testing for that. Signed char values correspond to the ascii table:

    http://www.idevelopment.info/data/Pr...ii_table.shtml

    As you can see, that is a single character ('a' or '-' or '3').
    Last edited by MK27; 03-09-2010 at 08:22 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 01:48 AM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21