Click here to Skip to main content
15,867,959 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
My instructor give us this code and we need to do algorithm and flowchart on this but i dont understand each line all i know is how it works. please help me just explain this..so i can make a flowchart and an algorithm..just an explanation of each line of this code thank you



C++
#include <stdio.h>

int main()
{
    int P, i, N, KEY;

    printf("\nArray size: ");
    scanf("%d", &N);

    int A[N];

    printf("\nPlease Enter %d elements of an Array \n", N);
    for (i = 0; i < N; i++)
    {
        scanf("%d", &A[i]);
    }

    printf("\nPlease Enter the location of a Element you want to insert\n");
    scanf("%d", &P);

    printf("\nPlease Enter the value of an Array Emenent to insert\n");
    scanf("%d", &KEY);

    int I = N - 1;

    while (I >= P - 1)
    {
        A[I + 1] = A[I];
        I = I - 1;
    }

    A[P - 1] = KEY;
    
    printf("\n Final Array after Inserting an  element is:\n");
    for (i = 0; i <= N; i++)
    {
        printf("%d\t", A[i]);
    }

    return 0;
}


What I have tried:

Ive tried searching in Google for explanation on this but sadly i got none.
Posted
Updated 17-Sep-21 18:07pm
v5

Assuming you wrote it then you should know what it does.

Alternatively, if you found it on the Internet because it seemed to match your homework assignment and want to use it for that, then that is a sure fire way to fail your course.

You are never going to be able to search Google for an explanation of random code samples, be sensible about what you can and cannot find by googling.

What you do need to do is learn C - via a good book or a good course preferably.

But I guess that is what you are currently doing and your assignment is to
- prompt a user for a list of values to insert into an array.
- Then prompt for another value to insert at a specific location within that array.
- Then print out the results.

That is what your code is doing.
 
Share this answer
 
The code allows the user to insert an item at choosen index (P-1) inside the array. To make room for the new item, existing items having index greater or equal to P-1 are moved at next position while the last one is discarded.

    insertion
before    after
[0]       [0]
[1]       [1]
..        ..
[P-1]     KEY
[P]       [P-1]     
[P+1]     [P]
..        [P+1]
..
[N-1]     [N-2]         
                   [N-1] discarded
 
Share this answer
 
Quote:
Ive tried searching in Google for explanation on this but sadly i got none.

Do not expect explanation for the full code, just for 1 line at the time, then you will have to put things together.

The code is a black box to you, open the box with a debugger.

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900