Question

C Programming Language Problem Title: Introduction to Database Database is a structurized method for data storage...

C Programming Language

Problem Title: Introduction to Database

Database is a structurized method for data storage and perform operations related to
the data. There are Some simple operations that can be done by a database are Insert,
Alter, Delete, and Query.
There are N different integers stored. You are asked to create a database where you can
carry out the following 4 operations :
1. I X - this operation is insert operation, inserting X to the database.
2. A X Y - Alter operation that alter data with value of X to Y .
3. D X - this operation is to delete X-valued data from the database.
4. Q X - Query operation, to count number of data that less than or equal to X.
It is also guaranteed that :
1. Insert will add data that is not exist in the database.
2. Alter operation will change data in the database to a new data that is not exist in
the database.
3. Delete operation will delete data from the database. The value that want to be
deleted always exist.
4. At most 250 operations for Insert, Alter, and Delete.

Format Input

Input consists of two integers N and K - number of data and number of operation. The
next line contains N integers as the initial data stored in the database. Then followed by
K lines each contains an operation command (I, A, D, or Q) and followed by an integer
X for operation I, D, Q or two integers X and Y for A operation.

Format Output

For each Q operation, output an integer that shows number of data that does not exceed
the query.

Constraints

• 1 ≤ N ≤ 100000
• 1 ≤ K ≤ 10000
• 0 ≤ X , Y ≤ 1000000000

Sample Input & Output (standard input & output)

6 7

2 3 4 5 6 7

Q 4

Case #1: 3

I 1

Q 4

Case #2: 4

D 2

Q 4

Case #3: 3

A 6 2

Q 4

Case #4: 4

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The source code is given below:


/*
* File: Database.c
*/

#include <stdio.h>
#include <stdlib.h>

#define N 100000
#define K 10000
#define X 1000000000
#define Y 1000000000

int n; /* Global variable for tracking total no of integers in the database*/
int data[N]; /*Global array of integers as database*/
int totQcase=0; /*Global variable for tracking total no of Query Case */
/* prototype declarations */
void insert(int x);
void alter(int x,int y);
void delete(int x);
void query(int x);
int main()
{
int k,x,y,i;
char op;
scanf("%d%d",&n,&k);// reading n and k
if((n>=1 && n<=N) && (k>=1 && k<=K))/* constraint check*/
{
for(i=0;i<n;i++)
scanf("%d",&data[i]); // reading initial data for data base

for(i=0;i<k;i++)
{
scanf(" %c",&op); // reading operation code

switch(op)
{
case 'I':
scanf("%d",&x); // reading operand x
if(x>=0 && x<=X)/* constraint check*/
{
insert(x); //insert operation called
}
else
printf("\ndata %d is out of range",x);
break;
case 'D':
scanf("%d",&x);// reading operand x
if(x>=0 && x<=X)/* constraint check*/
{
delete(x);//delete operation called
}
else
printf("\ndata %d is out of range",x);
break;
case 'Q':
scanf("%d",&x);// reading operand x
if(x>=0 && x<=X)/* constraint check*/
{
query(x);//query operation called
}
else
printf("\ndata %d is out of range",x);
break;
case 'A':
scanf("%d%d",&x,&y);// reading operand x and y
if((x>=0 && x<=X) && (y>=0 && y<=Y))/* constraint check*/
{
alter(x,y);//alter operation called
}
else
printf("\ndata %d or %d are out of range",x,y);
break;
}
}
}
else
printf("N or K is out of range");
printf("\n");
return (0);
}

void insert(int x)
{
int i;
for(i=0;i<n;i++){
if(data[i]==x)// if x found
break;
}
if(i==n)// if x not found
data[i]=x;// inserts x
  
n++;
  
}
void alter(int x,int y)
{
int i;
int xFound=0,yFound=0,xIndex=-1;
for(i=0;i<n;i++)
{
if(data[i]==x){// if x found
xFound=1;
xIndex=i;
break;
}
}
for(i=0;i<n;i++)
{
if(data[i]==y){// if y found
yFound=1;
break;
}
}
if(xFound && !yFound){ // if x found and y not found
data[xIndex]=y; // alters x with y
}
  
}
void delete(int x)
{
int i,pos=-1;
for(i=0;i<n;i++)
{
if(data[i]==x){ // if x found   
pos=i;
break;
}
}
if(pos!=-1){
for(i=pos;i<n-1;i++)
data[i]=data[i+1];//shifting operation
n--;
}
  
}
void query(int x)
{ int i,count=0;
totQcase++;
for(i=0;i<n;i++){
if(data[i]<=x)//counts data <= x
count++;
}
printf("\nCase #%d: %d",totQcase,count);//prints result
  
}

The source code screen shot is given below:

The output screen shot is given below:

Add a comment
Know the answer?
Add Answer to:
C Programming Language Problem Title: Introduction to Database Database is a structurized method for data storage...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming Language Problem Title: Jojo’s Hardest Problem (?) Jojo is enjoying his vacation with his...

    C Programming Language Problem Title: Jojo’s Hardest Problem (?) Jojo is enjoying his vacation with his family. Suddenly an instant message came in. Apparently there is a message from Lili : ”Jojo, don’t forget to check the forum. Who knows if there is an assignment before the exam” Jojo finally took the time to check the discussion forum and sure enough, there were some assignments given to his class. He immediately told his classmates. Jojo finds out one difficult problem...

  • C Programming Language Problem Title: Introduction to Computer Vision Jojo is an outstanding student who is...

    C Programming Language Problem Title: Introduction to Computer Vision Jojo is an outstanding student who is currently pursuing his Computer Science degree. In one of his Computer Vision lectures, his lecturer tackled a topic about binary thresholding. For simplicity, binary thresholding is an image filtering method used to separate low val- ued pixels (dark) from high valued pixels (bright). Each pixels in an image is classified as low or high based on a certain threshold K. If a pixel Pij...

  • C Programming Language Problem Title: Discount Jojo is browsing the internet while suddenly he sees an...

    C Programming Language Problem Title: Discount Jojo is browsing the internet while suddenly he sees an ad about the new cafe. The promotion is if the price of an item is N dollars, then you can buy the second item for half the price, the third item for a quarter of the original price, and so on, but if it becomes less than M dollars, then you have to pay M dollars. He wonders how much he has to pay...

  • Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each...

    Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; }; - INPUT i k : Insert the node with the key value of k in...

  • Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each...

    Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; };    - INPUT i k : Insert the node with the key value of k in...

  • C Programming Language Problem Title: Komodo National Park Do You know that Komodo National Park, located...

    C Programming Language Problem Title: Komodo National Park Do You know that Komodo National Park, located in Nusa Tenggara Timur (NTT), is a World Heritage Site by UNESCO, the specialized agency under United Nations (UN). One day, Lili and Bibi were on vacation on one of the islands in the Komodo National Park area and they just realized that they were living alone on that island because the cot- tage owner was completing his unfinished business on the main island....

  • C Programming Language Problem Title : Climbing Stairs Bibi climbs stairs of a multi-leveled building. Every...

    C Programming Language Problem Title : Climbing Stairs Bibi climbs stairs of a multi-leveled building. Every time Bibi climbs a set of stairs, she counts the steps starting from 1 to the number of steps in that particular set of stairs while climbing the stairs. For example if she climbs two set of stairs, the first containing 5 steps and the second containing 3 steps, she will say 1, 2, 3, 4, 5, 1, 2, 3 and the total number...

  • C language Thank you Jojo is going to a restaurant. There are N foods listed in...

    C language Thank you Jojo is going to a restaurant. There are N foods listed in the menu, and the items are sorted increasingly based on its price. Now Jojo is wondering how many foods are there with price P. As the number of food in the menu can be a lot, Jojo will need your help to answer his questions. Jojo knows you can count really fast, so he will give you M questions. Format Input Input begins with...

  • C Programming Language The code below matches the sample input/output but is marked wrong. Are there...

    C Programming Language The code below matches the sample input/output but is marked wrong. Are there other ways to do this problem? The focus is on recursion and functions. #include<stdio.h> int joCheck(unsigned long long int number) { if(number % 7 == 0 || number % 8 == 0) { return 1; } else return 0; } int main() { int cases = 0; scanf("%d", &cases); for(int i = 1; i<=cases; i++) { unsigned long long int number; scanf("%d", &number); if(joCheck(number)...

  • in C++ Code should work for all cases In this assignment you are requested to implement...

    in C++ Code should work for all cases In this assignment you are requested to implement insert, search, and delete operations for an open-addressing hash table with double hashing. Create an empty hash table of size m= 13. Each integer of the input will be a key that you should insert into the hash table. Use the double hashing function h{k, i) = (hı(k) + ih2(k)) mod 13 where hi(k)= k mod 13 and h2(k) = 1+(k mod 11). The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT