[SOLVED]Algorithm & Flowchart Design

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
That should be more than enough for you to start writing the code and debugging from there.
Ok I've written code but its different from flowchart. I've verified code. It give exactly same result as I want

C:
/*
* Program:   Add and serach number in list
* Author:    Pushkar
* Created on May 5, 2021, 5:59 AM
*/

#include<stdio.h>

int main (void)
{
    int i, N, X;
    
    printf("How many numbers you want to add : " );
    scanf("%d",&N);                                  // Get size of list       
    
    int List[N];                                     // declare array for list
        
    
    for(i=0; i<N; i++)                               
    {
        printf("print List element : ", List[i]);
        scanf("%d",&List[i]);                        // add numbers in list
    }
    
    printf("Which number you want to search : " );
    scanf("%d",&X);                                 // Get number
    
    
    for(i= 0; i<N; i++)
    {
        if (X == List[i])                           // Check if number match with list numbers   
        {
            printf("Number Found in List \n");     
            break;
        }   
    
    }
    
    return 0;

}

How many numbers you want to add : 3
print List element : 1
print List element : 2
print List element : 3
Which number you want to search : 1
Number Found in List

How many numbers you want to add : 3
print List element : 1
print List element : 2
print List element : 3
Which number you want to search : 2
Number Found in List

How many numbers you want to add : 3
print List element : 1
print List element : 2
print List element : 3
Which number you want to search : 3
Number Found in List

@click_here I don't know how to delete any number in list. When we declare array we allocate fixed size of memory We can't add or delete allocated memory for array
 

click_here

Joined Sep 22, 2020
548
Ok I've written code but its different from flowchart. I've verified code. It give exactly same result as I want

C:
/*
* Program:   Add and serach number in list
* Author:    Pushkar
* Created on May 5, 2021, 5:59 AM
*/

#include<stdio.h>

int main (void)
{
    int i, N, X;
   
    printf("How many numbers you want to add : " );
    scanf("%d",&N);                                  // Get size of list      
   
    int List[N];                                     // declare array for list
       
   
    for(i=0; i<N; i++)                              
    {
        printf("print List element : ", List[i]);
        scanf("%d",&List[i]);                        // add numbers in list
    }
   
    printf("Which number you want to search : " );
    scanf("%d",&X);                                 // Get number
   
   
    for(i= 0; i<N; i++)
    {
        if (X == List[i])                           // Check if number match with list numbers  
        {
            printf("Number Found in List \n");    
            break;
        }  
   
    }
   
    return 0;

}

How many numbers you want to add : 3
print List element : 1
print List element : 2
print List element : 3
Which number you want to search : 1
Number Found in List

How many numbers you want to add : 3
print List element : 1
print List element : 2
print List element : 3
Which number you want to search : 2
Number Found in List

How many numbers you want to add : 3
print List element : 1
print List element : 2
print List element : 3
Which number you want to search : 3
Number Found in List

@click_here I don't know how to delete any number in list. When we declare array we allocate fixed size of memory We can't add or delete allocated memory for array
You could use a -1 as a place holder that you don't print, or use malloc to set up the array and realloc to resize it.

However, the best way would be to make a linked list: this is one of the cases where it is perfect for (regularly adding/deleting items in a list)
 

click_here

Joined Sep 22, 2020
548
You could also have a larger array and keep track of the length of how many elements you are using.

You'd have to do a quick check when adding numbers to say if there is not enough memory.

Here is a *very* basic bare bones way of using this method for a delete (after everything is already set up)
C:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int numberSet[10] = {1,2,3,4,5,6};
    int currentLength = 6;
    int numberToRemove = 3;
    int i, src, dest;
    
    printf("Before: ");
    for(i=0; i<currentLength; i++)
    {
        printf("%d ", numberSet[i]);
    }
    putchar('\n');
    
    // Remove number
    for(src=0, dest=0; src<currentLength; src++)
    {
        if(numberSet[src] == numberToRemove)
        {
            continue;
        }
        
        numberSet[dest++] = numberSet[src];
    }  
    currentLength--;
    
    printf("After: ");
    for(i=0; i<currentLength; i++)
    {
        printf("%d ", numberSet[i]);
    }
    putchar('\n');
    
    
    return EXIT_SUCCESS;
}
 
Top