Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Declare and initialize arrays/variables Display menu. 1. Create a dynamic array

ID: 3635057 • Letter: D

Question

Declare and initialize arrays/variables
Display menu.
1. Create a dynamic array of random elements (array size indicated by the user) and write it into a file indicated by the user.
2. Load a dynamic array of elements from a file indicated by the user
3. Sort the array
4. Display the array
5. Exit

If menu choice 1 Call a function to populate the array:

Ask the user for the number of elements (size of the array)
Ask the user for a seed (a seed is any positive integer)
Generate a dynamic array of random numbers from the set
{5, 10, 15, 20, …, 95, 100}

Ask the user for file name
Call a function to write the content of the array into the specified file.
Display a prompt for successful operation

Else if menu choice 2

Ask the user for the file name
Call a function to load the content of the file into a dynamic array of elements
Display a prompt for successful operation

Else if menu choice 3
Call a selection sort function to sort the current array

Else if menu choice 4
Call a function to display the content of the current array on a single line with elements separated by | (Important: no | after the last element)

Else if menu choice 5
Display an exit message

Else
Display an error on input prompt
If not an exit condition
Display the menu again


No global variables/arrays are allowed.

Explanation / Answer

Dear,



#include "stdafx.h"
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<conio.h>

//funtion prototypes
void funtion1();
void funtion2();
void funtion3();
void selectionSort(int[],int);
void display();
void display();

//main starts here

int main()
{
//menu

    int ch;
    do{
    printf("MENU ");
    printf("1.calling funtion and writing file ");
    printf("2.Ask for a file name ");
    printf("3.funtion Selection sort ");
    printf("4.Display Array ");
    printf("5.exit ");
    printf("Enter your choice: ");
    scanf("%d",&ch);
}while(ch>5);
//To select suitable mehod
    switch(ch)
    {
    case 1:funtion1();break;
    case 2:funtion2();break;
    case 3:funtion3();break;
    case 4:display();break;
    case 5:exit(1);break;
    }
    getch();
    return 0;
}

//To display the array write to file
void display()
{
    char fileName[20];
    int i,size=0;
    int *arr;
    FILE *fp;
    printf("Enter a file name");
    scanf("%s",fileName);
    fopen(fileName,"r");
    if(!fp)
    {
        printf("file not exist");
        getch();
    }
    while(!feof(fp))
    {
        fscanf(fp,"%5d",arr[size]);
        size++;
    }

//Calling selectionSort funtion
    selectionSort(arr,size);

    for(i=0;i<size;i++)
        printf("%d",arr[i]);
}

//funtion to to sort file elements using seleciton sort and display
void funtion3()
{
    char fileName[20];
    int i,size=0;
    int *arr;
    FILE *fp;
    printf("Enter a file name");
    scanf("%s",fileName);
    fopen(fileName,"r");
    if(!fp)
    {
        printf("file not exist");
        getch();
    }
    while(!feof(fp))
    {
        fscanf(fp,"%5d",arr[size]);
        size++;
    }
    selectionSort(arr,size);
}
//selection sort implementation
void selectionSort(int arr[],int size)
{
    int sScan,minIndex,minValue,index;
    for(sScan=0;sScan<(size-1);sScan)
    {
        minIndex=sScan;
        minValue=arr[sScan+1];
        for(index=sScan+1;index<size;index++)
        {
            if(arr[index]<minValue)
            {
                minValue=arr[index];
                minIndex=index;
            }
        }
        arr[minIndex]=arr[sScan];
        arr[sScan]=minValue;
    }
}

//funtion to open file and read data into into array

void funtion2()
{
    char fileName[20];
    int i;
    int *arr;
    FILE *fp;
    printf("Enter a file name");
    scanf("%s",fileName);
    fp=fopen(fileName,"r");

    while(!feof(fp))
    {
        fscanf(fp,"%5d",arr[i]);
        i++;
    }
    printf("Successfully completed");
}

//Funtion to get dynamic array size and initialize with random values

//write to file specified by user
void funtion1()
{
    int n,s,i;
    int *arr;
    int r;
    FILE *fp;
    char fileName[20];
    int randomValues[]={5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100};
    printf("Enter number of elements");
    scanf("%d",&n);
    printf("Enter seed value");
    scanf("%d",&s);
    srand(s);
    arr=(int*)malloc(n*sizeof(n));

    for(i=0;i<n;i++)
    {
        r=rand()%20+1;
        arr[i]=randomValues[r];
    }

    printf("Enter a file name");
    scanf("%s",fileName);

    fp=fopen(fileName,"w");
    for(i=0;i<n;i++)
        fprintf(fp,"%5d",arr[i]);
    printf("Successful");

}

Hope this would helpful to you..

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote