A few complicated progams to solve

+1 Tom Bouhraoua · December 24, 2014
Hello to all.

I'm new in programming but I'm doing my best to keep up with things I need to learn for school so I'm watching all tutorials from Bucky and I must admit they are pretty awesome and interesting.

Although, I'm currently on tutorial 20 (or something like that) but I need some more complicated programs for my homework so I decided to ask for your help.
I would try to do this by myself, but I have only two more days and I think I'm not capable to learn all things needed by then.

I will apreciate if anyone could do at least one of them.
I don't know rules of this forum, but I hope you won't find this as too big request and that you will help me. :)


So let's start with programs.

1. Write function with this prototype:
int f1(int *p1, int *p2)
User should enter first number of elements of array, then elements, then index of p1 and p2.

Function gets two pointers which point on two elements on  array (which is integer). This function should return "TRUE" if all elements of array between *p1 and *p2 are sorted from the lowest to the biggest (including p1 and p2). If this condition isn't true, then function should return "FALSE".
Also, if p1 and p2 point to same element of array function should return "TRUE", if p2 points to the element in array which is before element of p1, function should return "FALSE".

Also program needs main function which allows entering array of at most 100 elements.
Elements of array are integer.
If user enters input index which is lower than 0 or >= number of elements, program should write "Error" and close.
For example (output):
Number of elements: 10
Elements: 5 1 7 2 3 4 5 5 5 4
Index p1: 3
Index p2: 6
TRUE

Number of elements: 7
Elements: 7 8 9 10 11 11 12
Index p1: 0
Index p2: 5
FALSE



2. Write function with this prototype:
int f2(unsigned int a, unsigned int b)
Function gets two numbers from user (a i b, type: unsigned int) and returns "TRUE" if b is contained in a and returns "FALSE" if not.

For example (output):
Enter a: 3469
Enter b: 46
TRUE

Enter a: 3469
Enter b: 9
TRUE

Enter a: 3469
Enter b: 334
FALSE

Enter a: 3469
Enter b: 3469
TRUE


3. Program works with matrix 30x30.
Program should have this output:

Menu:
1. Dot
2. Line
3. Rectangle
4. Circle
5. Output
6. Exit
Input your choice: 1
Input coordinate of x: 5
Input coordinate of y: 5
Input character: +

Menu:
1. Dot
2. Line
3. Rectangle
4. Circle
5. Output
6. Exit
Input your choice: 3
Input coordinate of x (top left angle): 7
Input coordinate of y (top left angle): 2
Input coordinate of x (bottom right angle): 12
Input coordinate of y (bottom right angle): 5
Input character: =

Menu:
1. Dot
2. Line
3. Rectangle
4. Circle
5. Output
6. Exit
Input your choice: 2
Input coordinate of x (start of line): 10
Input coordinate of y (start of line): 10
Input coordinate of x (end of line): 15
Input coordinate of y (end of line): 5
Input character: *

Menu:
1. Dot
2. Line
3. Rectangle
4. Circle
5. Output
6. Exit
Input your choice: 5
/images/forum/upload/2014-12-24/7bde99c31c71bb765507c9103e1b83e1.jpg

Menu:
1. Dot
2. Line
3. Rectangle
4. Circle
5. Output
6. Exit
Input your choice: 6
END OF PROGRAM.



Thanks in advance and sorry for bad English,
Tom.

Post a Reply

Replies

Oldest  Newest  Rating
0 Tom Bouhraoua · December 25, 2014
First to ask you, can you solve this function f2 in any simpler way, is it possible?

And for f1 (output):

Number of elements: 5
Elements: 6 7 8 9 0
Index p1: 1
Index p2: 3
TRUE

Number of elements: 7
Elements: 5 6 9 2 3 4 5
Index p1: 2
Index p2: 6
FALSE


So, program should compare elements between two numbers on which pointers point (including those two numbers) and if every next element in array is bigger than number before, program should write TRUE.
0 c student · December 25, 2014
could you provide more example output for the first function?
0 Tom Bouhraoua · December 25, 2014
f1 is used in first program, it isn't needed for program 2.

Thank you for effort, but I can't use this because we can't use strings, assert or enum. I don't even know what that is. We are at functions currently and pointers so program should look much more simpler...
0 c student · December 24, 2014
here is f2, though you might need to add an error check for valid a and b number range
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

typedef enum {
    FALSE,
    TRUE,
} boolean;

void testf2 ();
int f2 (unsigned int, unsigned int);

int main() {
    
    unsigned int a;
    unsigned int b;
    
    testf2();
    
    int err = scanf("%u%u", &a, &b);
    if (err != 2 || err == -1) {
        fprintf (stderr, "Input error\n");
        abort();
    }
    f2 (a, b);

    return 0;
}

// function to test function f2
void testf2 () {
    
    assert (f2(492383, 92) == 1);
    assert (f2(204, 92) == 0);
    assert (f2(3, 92) == 0);
    assert (f2(34, 34) == 1);
    assert (f2(130, 13) == 1);
    assert (f2(200000000, 000) == 1);
    assert (f2(258703, 25870) == 1);
    assert (f2(69, 6) == 1);
    
}

int f2 (unsigned int a, unsigned int b) {
    
    // default value
    boolean result = FALSE;
    
    // printing numbers into strings
    char str1[12];
    if (sprintf (str1, "%d", a) < 0) {
        fprintf (stderr, "Argument error\n");
        abort();
    }
    char str2[12];
    if (sprintf (str2, "%d", b) < 0) {
        fprintf (stderr, "Argument error\n");
        abort();
    }
    
    // finding substring of string
    if (strstr (str1, str2) != NULL) {
        result = TRUE;
    }
    
    return result;
}


i'm not exactly sure what you mean in your description of function 1, perhaps you could clarify?
  • 1

C

107,323 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator