CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2016
    Posts
    3

    Need help with a small program

    I am a programmer in learning and i have a problem.

    I use visual studio 2015 and in language 'C' i have to make a program which entered array of char's sorts into ascending order, prints out only Roman numerals out of it ('I','X','V'...) and an array without those numerals. Total of 3 prints. I would really appreciate if someone would write down the code since i tried many times and failed to make it. Also try using only for's and if's since i can use those only .

    Code example:

    Code:
    #include <stdio.h>  
    #define MAX_BR_EL 15
    void main()
    {
     int i, p, j, s, n;
     while (1) {
        printf("Enter the array length:\n");
        scanf("%d", &n);
        if (n <= 0 || n > MAX_BR_EL) break;
        char a[n];
        printf("Enter the array:\n");
        for (s = 0; s < n; scanf("%c", &a[s++]));
        {
            for (i = 0; i < n - 1; i++)
                for (j = i + 1; j < n; j++)
                    if (a[i] < a[j]) { p = a[j], a[j] = a[i], a[i] = p; }
        }
            printf("Arranged array:\n", a[s]);
        }
        if (a[s] == 'I' || a[s] == 'V' || a[s] == 'X' || a[s] == 'L' || a[s] == 'C' || a[s] == 'D' || a[s] == 'M')
        {
            printf("Array of Roman numerals:\n");
            printf("%c\n", a[s]);
        }
        else
        {
            printf("Array without Roman numerals:\n");
            printf("%c\n", a[s]);
        }
    
    
     }
    
    
     }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,551

    Re: Need help with a small program

    The size of an array has to be defined at compile-time, not run-time.

    As array a is to be used outside of the while loop, it needs to be defined outside as well.

    Code:
    printf("Arranged array:\n", a[s]);
    This is not how you display the array. You'll need a loop to display the array elements.

    Your test for roman numerals / display is also outside of any loop
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.11.5)

  3. #3
    Join Date
    Apr 2016
    Posts
    3

    Re: Need help with a small program

    I tried putting this :

    Code:
    printf("Arranged array:");
    			for (s = 0; s < n; s++)
    			printf("%c\n", &a[s])
    but the prints are a mess.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,624

    Re: Need help with a small program

    The ampersand as used there gives you the address of a variable.

    You should get out of the habit of single character, meaningless variable names, and putting multiple statements on a single line. It makes code impossible to understand and debug.

  5. #5
    Join Date
    Apr 2016
    Posts
    3

    Re: Need help with a small program

    Thanks,did not notice that. Also is my comparison code good? Have you noticed anything else that needs fixing?

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,551

    Re: Need help with a small program

    Code:
    printf("Arranged array:");
    for (s = 0; s < n; ++s)
         printf("%c", a[s]);
    For roman
    Code:
    const char roman[] = "IVXLCDM";
    ...
    if (strchr(roman, a[s]))
    		//Got a roman
    	else
    		//Not a roman
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.11.5)

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,551

    Re: Need help with a small program

    Have you noticed anything else that needs fixing?
    Why the while loop? If this is for looping to repeat ask for input and display until invalid input and exit then the loop scope is wrong - otherwise what it's purpose?

    Code:
    obtain data
    sort data
    display as required
    PS how are you dealing with the \n entered as part of the array length?
    Last edited by 2kaud; April 7th, 2016 at 01:15 PM. Reason: PS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.11.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured