The basics of programming in c++ for beginners

Tasks and solution. С

  1. Data types, variables and constants
  2. Ввод с клавиатуры и вывод данных на экран
  3. Арифметические операции и комбинированные операторы
  4. Операторы выбора if и else в С
  5. The switch statement in C ++
  6. The for loop in C++
  7. Циклы while, do while, nested loops
  8. Two-dimensional arrays C ++
  9. Arrays in C++
  10. Functions in C++
  11. Pointers C ++
  12. A pointer to a pointer to C ++
  13. Classes in C ++

Additional collection of problems with solutions

практика программирования, задачи с решением, задачи по программированию с++, задачи по программированию C++

This page contains all of the tasks on the topics lessons, posted on our website. К каждой задаче прилагается наш вариант решения. Постарайтесь всё же решать их самостоятельно и только потом сравнивать наш исходный код со своим. “Хочешь научиться программировать – программируй”!

Больше практических заданий с решениями найдёте here.

Рекомендуем посетить Сайт для юных программистов – где вы найдете уроки по различным языкам программирования (в том числе для детей), 3D-моделированию, Linux и др.

128 thoughts on “Tasks and solution. С

  1. Hello,I need help writing code in C for the C8051F38 microcontroller with LCD screen ,you need to connect a 7-segment display to the microcontroller(common anode) via button,when turned on, the display and screen should display 0, when you press the button, it should count forward to 9 with a delay of 500ms,when the button is released, the display saves the last number shown ,with the next clamping, the countdown continues from the place where it stopped and so on in a circle,the countdown should be displayed in parallel on the LCD screen with the inscription Status:running in green when the countdown is in progress,and State :stop red when it stops,I tried,but it doesn't work
    #include
    #include
    #include
    #define BUTTON_PIN PB0
    #define SEGMENT_A_PIN PD0
    #define SEGMENT_B_PIN PD1
    #define SEGMENT_C_PIN PD2
    #define SEGMENT_D_PIN PD3
    #define SEGMENT_E_PIN PD4
    #define SEGMENT_F_PIN PD5
    #define SEGMENT_G_PIN PD6

    #define SEGMENT_PORT PORTD
    #define SEGMENT_DDR DDRD

    #define BUTTON_PORT PORTB
    #define BUTTON_DDR DDRB

    // Symbol table for 7-segment indicator (common anode)
    const unsigned char SEGMENT_DIGITS[] = {
    0b00001000, // 0
    0b00111110, // 1
    0b00100100, // 2
    0b00011100, // 3
    0b01001110, // 4
    0b01100100, // 5
    0b01111000, // 6
    0b00000110, // 7
    0b01111110, // 8
    0b01101100, // 9
    };

    // Variables
    volatile uint8_t current_digit = 0;
    volatile uint8_t button_state_prev = 1; // Initialization how “released” to avoid false clicks

    void display_digit(uint8_t digit) {
    SEGMENT_PORT = ~SEGMENT_DIGITS[digit]; // Invert values ​​for common anode
    }

    void update_display() {
    display_digit(status == 0 ? 0 : current_digit);
    }

    void handle_button_press() {
    if (status == 0) {
    status = 1;
    current_digit = 0;
    } else {
    current_digit = (current_digit + 1) % 16;
    }
    update_display();
    }

    void handle_button_release() {
    if (status == 1) {
    status = 0;
    }
    }

    void setup() {
    // Configuring a button as a pull-up resistor input
    BUTTON_DDR &= ~(1 << BUTTON_PIN);
    BUTTON_PORT |= (1 << BUTTON_PIN);

    // Configuring Segment Pins as Outputs
    SEGMENT_DDR |= (1 << SEGMENT_A_PIN) |
    (1 << SEGMENT_B_PIN) |
    (1 << SEGMENT_C_PIN) |
    (1 << SEGMENT_D_PIN) |
    (1 << SEGMENT_E_PIN) |
    (1 << SEGMENT_F_PIN) |
    (1 << SEGMENT_G_PIN);
    }

    void loop() {
    // Reading button state with edge detection (volatile to update the flag)
    volatile uint8_t button_state = PINB & (1 << BUTTON_PIN);

    // Processing a Button Press with Edge Detection
    if (!button_state && button_state_prev) {
    handle_button_press();
    }

    // Button release processing with edge detection
    if (button_state && !button_state_prev) {
    handle_button_release();
    }

    // Update display
    update_display();

    // Delay 500 ms (replace with the appropriate delay function)
    _delay_ms(500);

    // Updating a button's previous state
    button_state_prev = button_state;
    }

    int main() {
    setup();
    loop();
    return 0;
    }

Leave a Reply to Atabek Cancel reply

Your email address will not be published. Required fields are marked *