Yam Code
Sign up
Login
New paste
Home
Trending
Archive
English
English
Tiếng Việt
भारत
Sign up
Login
New Paste
Browse
#include <stdio.h> #include <stdlib.h> //printf("\ntest %d\n", index); int intInput(); void function1(int*, int&); void function2(int*, int, int&); void addValue(int*, int&); void display(int*, int); void search(int*, int); void swap(int&, int&); int delet(int*, int, int&); void remove1(int*, int&); void removeAll(int*, int&); void increase(int*, int); void decrease(int*, int); int main(){ int *arr; int index = 0; arr = (int*)malloc(index * sizeof(int)); function1(arr, index); printf("Thankyou for using my program!"); free(arr); return 0; } int intInput(){ int i, output, check; char ch; while(1){ check = scanf("%d%c", &output, &ch); fflush(stdin); if(check != 2 || ch != 10) printf("wrong input!\n"); else return output; } } void function1(int *arr, int &index){ int choice; while(1){ printf("1- Add a value\n"); printf("2- Search a value\n"); printf("3- Remove the first existence of a value\n"); printf("4- Remove all existences of a value\n"); printf("5- Print out the array\n"); printf("6- Sort the array in ascending order (positions of elements are preserved)\n"); printf("7- Sort the array in descending order (positions of elements are preserved)\n"); printf("Others- Quit\n"); choice = intInput(); if(choice < 1 || choice > 7) break; else function2(arr, choice, index); printf("--------------------------------\n"); system("pause"); } } void function2(int *arr, int choice, int &index){ switch (choice){ case 1: addValue(arr, index); break; case 2: search(arr, index); break; case 3: remove1(arr, index); break; case 4: removeAll(arr, index); break; case 5: display(arr, index); break; case 6: increase(arr, index); break; case 7: decrease(arr, index); break; } } void addValue(int *arr, int &index){ index++; arr = (int*) realloc(arr, index * sizeof(int)); printf("Input an integer to store: "); arr[index-1] = intInput(); } void display(int *arr, int index){ printf("All you current array:"); for(int i = 0; i < index; i++) printf("\t%d", arr[i]); printf("\n"); } void search(int *arr, int index){ int number, i; printf("What number you wanna search? "); number = intInput(); for(i = 0; i < index; i++){ if(number == arr[i]){ printf("Your Number is in slot number %d in array.\n", i+1); break; } } if(i == index) printf("Your Number not found.\n"); } void swap(int &a, int &b){ int temp = a; a = b; b = temp; } int delet(int *arr, int deletNum, int &index){ int detector = NULL; for(int i = 0; i < index; i++){ //locating the delete number's index in array if(deletNum == arr[i]){ detector = i; break; } } if(detector != NULL){ arr[detector] = 0; for(int i = detector; i < index; i++) swap(arr[i], arr[i+1]); index--; arr = (int*) realloc(arr, index * sizeof(int)); return 1; } else return 0; } void remove1(int *arr, int &index){ int deletNum; printf("What number you wanna remove? "); deletNum = intInput(); if(delet(arr, deletNum, index)){ printf("Removed!\n"); display(arr, index); } else printf("Your number not found!\n"); } void removeAll(int *arr, int &index){ int deletNum, uselessAlert = 0; printf("What number you wanna remove? "); deletNum = intInput(); for(int i = 0; i < index; i++){ if(delet(arr, deletNum, index)){ uselessAlert++; } } if(uselessAlert == 0) printf("Your number not found!\n"); else printf("Removed all!\n"); display(arr, index); } void increase(int *arr, int index){ int copy[index]; for(int i = 0; i < index; i++) copy[i] = arr[i]; for(int i = 0; i < index-1; i++) for(int j = i+1; j < index; j++) if(copy[i] > copy[j]) swap(copy[i], copy[j]); display(copy, index); } void decrease(int *arr, int index){ int copy[index]; for(int i = 0; i < index; i++) copy[i] = arr[i]; for(int i = 0; i < index-1; i++) for(int j = i+1; j < index; j++) if(copy[i] < copy[j]) swap(copy[i], copy[j]); display(copy, index); }
Paste Settings
Paste Title :
[Optional]
Paste Folder :
[Optional]
Select
Syntax Highlighting :
[Optional]
Select
Markup
CSS
JavaScript
Bash
C
C#
C++
Java
JSON
Lua
Plaintext
C-like
ABAP
ActionScript
Ada
Apache Configuration
APL
AppleScript
Arduino
ARFF
AsciiDoc
6502 Assembly
ASP.NET (C#)
AutoHotKey
AutoIt
Basic
Batch
Bison
Brainfuck
Bro
CoffeeScript
Clojure
Crystal
Content-Security-Policy
CSS Extras
D
Dart
Diff
Django/Jinja2
Docker
Eiffel
Elixir
Elm
ERB
Erlang
F#
Flow
Fortran
GEDCOM
Gherkin
Git
GLSL
GameMaker Language
Go
GraphQL
Groovy
Haml
Handlebars
Haskell
Haxe
HTTP
HTTP Public-Key-Pins
HTTP Strict-Transport-Security
IchigoJam
Icon
Inform 7
INI
IO
J
Jolie
Julia
Keyman
Kotlin
LaTeX
Less
Liquid
Lisp
LiveScript
LOLCODE
Makefile
Markdown
Markup templating
MATLAB
MEL
Mizar
Monkey
N4JS
NASM
nginx
Nim
Nix
NSIS
Objective-C
OCaml
OpenCL
Oz
PARI/GP
Parser
Pascal
Perl
PHP
PHP Extras
PL/SQL
PowerShell
Processing
Prolog
.properties
Protocol Buffers
Pug
Puppet
Pure
Python
Q (kdb+ database)
Qore
R
React JSX
React TSX
Ren'py
Reason
reST (reStructuredText)
Rip
Roboconf
Ruby
Rust
SAS
Sass (Sass)
Sass (Scss)
Scala
Scheme
Smalltalk
Smarty
SQL
Soy (Closure Template)
Stylus
Swift
TAP
Tcl
Textile
Template Toolkit 2
Twig
TypeScript
VB.Net
Velocity
Verilog
VHDL
vim
Visual Basic
WebAssembly
Wiki markup
Xeora
Xojo (REALbasic)
XQuery
YAML
HTML
Paste Expiration :
[Optional]
Never
Self Destroy
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Status :
[Optional]
Public
Unlisted
Private (members only)
Password :
[Optional]
Description:
[Optional]
Tags:
[Optional]
Encrypt Paste
(
?
)
Create New Paste
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Site Languages
×
English
Tiếng Việt
भारत