Archives

Array

Share

Click

  
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#define SIZE 100

void main()
{

char ch;
int size=0, a[SIZE], i=0, b=0, temp=0, x=0, w, largest=0, largest2=0, q;
int c;
do{

printf("Enter how many numbers you want to input: ");
scanf("%d",&size);
printf("Enter %d numbers you want to enter: ", size);
for(i=0;i<size;i++){
scanf("%d", &a[i]);

}

printf("\n");
printf("\n");
printf("This are the numbers u entered: ");
for(b=0;b<size;++b){
printf(" %d", a[b]);
}

//largest
for(w=0;w<size;w++){
if(largest<a[w])
largest=a[w];

}

printf("\nThis is the largest number you entered: %d", largest);

//2nd largest
for(q=0;q<size;q++){
if(largest>a[q]){
if(largest2<a[q]){
largest2=a[q];
}}
}

printf("\nThis is the second largest number you entered: %d", largest2);

//sorted
for(i=0;i<size;i++){
for(b=0;b<(size-(i+1));b++){
if (a[b]>a[b+1]){
temp=a[b];
a[b]=a[b+1];
a[b+1]=temp;
}}}
printf("\nThis are the sorted numbers: ");
for(x=0;x<size;++x){
printf(" %d", a[x]);
}

printf("\n");

printf("\nRun again?(y/n)");
ch = getch();

if(ch=='y'||ch=='Y'){
c=1;
system("cls");
}
else{
c=0;
}

fflush(stdin);
}while(c==1);


}

Strings

Share

  
void input(char str[]){
gets(str);
}

void display(char str[]){
int i;
for(i=0;str[i]!='\0';i++)
printf("%c ",str[i]);
printf("\n");
}

int countA(char str[]){
int i,count=0;
for(i=0;str[i]!='\0';i++){
if(str[i]=='a'||str[i]=='A')
count++;
}
return count;
}

int findItem(char array[],int size,char item){
int i,found=0;
for(i=0;array[i]!='\0';i++){
if(array[i]==item){
found=1;
break;
}
}
return found;
}

int isPalindrome(char str1[]){
int i,size=strlen(str1)-1;
int yes=1;
for(i=0,j=size-1;i<=size/2;i++,j--){
if(str1[i]!=str1[j]){
yes=0;
break;
}
}
return yes;
}

int countVowel(char str[]){
int i,count=0;
for(i=0;str[i]!='\0';i++){
//val=toupper(str[i]);
//if(val=='A'||val=='E'||val=='I'||val=='O'||val=='U')
if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'
||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||str[i]=='u'||str[i]=='U')
count++;
}
return count;
}

MAIN CODE
#include<stdio.h>
#include"strings.h"

int main(void){
char string1[30]="hello world";
input(string1);
display(string1);
printf("%d\n",countA(string1));
return 0;
}

Set 0.2

Share

  
void display(int set[],int count){
int i;
printf("{");
for(i=0;i<count;i++)
printf("%d,",set[i]);
printf("\b}\n");
}

void add(int set[],int *count,int item){
if(!(contains(set,*count,item)))
set[(*count)++]=item;
}

int contains(int set[],int count,int item){
int i,found=0;
for(i=0;i<count;i++){
if(set[i]==item){
found=1;//return 1;
break;
}
}
return found;//return 0;
}

Array Operations - Source Code

Share

#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include "ARRAY1.h"
#include "MYTOOLS.h"
#define SIZE 100



void main(){

int ray=0, la[SIZE],bh=0, ho=0, ebe, ri, t, p, r=1, g;
char sw, c, ch;


do{
drawbox();
printf("\nEnter your choice: ");
scanf("%s", &sw);
switch(sw){
case 'a':
case 'A':
printf("\nEnter how many numbers you want to input: ");
scanf("%d", &ray);
printf("\nEnter the numbers: ");
createArray(la, ray);
printf("\nSuccessfuly created an array.\nNow press b to view the numbers you entered.");
printf("\n ");

break;
case 'b':
case 'B':
if(la[SIZE]=='\0')
printf("Array is empty.");
else{
printf("These are the items in your array: ");
printArray(la, ray);
}

break;
case 'c':
case 'C':
if(la[SIZE]=='\0')
printf("Array is empty.");
else{
printf("Enter the number you want to ensert: ");
scanf("%d", &bh);
printf("At what position do you want it to be inserted: ");
scanf("%d", &ho);
if(inserAtPos(la, ray, bh, ho)==1){
ray=ray+1;
printf("Item successfuly inserted!");
}}

break;
case 'd':
case 'D':
if(la[SIZE]=='\0')
printf("Array is empty.");
else{
printf("Enter the number you want to insert in front: ");
scanf("%d", &bh);
insertFront(la, ray, bh);
ray=ray+1;
printf("Item successfuly inserted in front of the list!");
}

break;

case 'e':
case 'E':
if(la[SIZE]=='\0')
printf("Array is empty.");
else{

while(r==1){
for(p=0; p<ray;p++){
printf("\n(position %d)-- %d", p+1, la[p]);

}

printf("\nEnter the position of the item you want to remove: ");
scanf("%d", &ebe);
if (ebe<ray+1){
r=0;
removeAtPos(la, ray, ebe);
ray=ray-1;
printf("Item successfuly removed at the given position!");

}
else{
printf("\n");
printf("Enter a correct position!");
printf("\n");
}
}
}
r=1;
break;

case 'f':
case 'F':
if(la[SIZE]=='\0')
printf("Array is empty.");
else{
printArray(la, ray);
printf("\nEnter the item you want to remove: ");
scanf("%d", &ri);
if(removeItem(la, ray, ri)==1){
ray=ray-1;
printf("Item successfuly removed!");
}
else
printf("Item not in the list!");
}
break;

case 'g':
case 'G':
if(la[SIZE]=='\0')
printf("Array is empty.");
else{
if(removeFront(la, ray)==1)
ray=ray-1;
}
break;

case 'h':
case 'H':
if(la[SIZE]=='\0')
printf("Array is empty.");
else{
printf("Enter the item you want to search if its in the list: ");
scanf("%d", &t);
locateIndex(la, ray, t);
}

break;

case 'j':
case 'J':
if(la[SIZE]=='\0')
printf("Array is empty.");
else{
sortAscending(la, ray);
sortDescending(la, ray);
}

break;

case 'k':
case 'K':
exit(0);

break;

case 'i':
case 'I':
printf("Enter the item you want to search if its in the list: ");
scanf("%d", &g);
locateItem(la, ray, g);


break;

}
printf("\n");
printf("\nRun again?(y/n)");
ch = getch();

if(ch=='y'||ch=='Y'){
c=1;
system("cls");
}
else{
c=0;
}

fflush(stdin);
}while(c==1);

}

Array Operations (ARRAY1.h)

Share

void createArray(int la[], int n){
int i, d;
for (i=0;i<n;i++){
if(i<1)
scanf("%d", &la[i]);

else{
scanf("%d", &la[i]);
for(d=1;d<i+1;d++){
if(la[i]==la[i-d]){
printf("The number you entered is already in the list. Enter another no.: ");
i--;}}
}
}
}


void printArray(int la[],int n){

int i;
for (i=0;i<n;i++){
printf("%d ", la[i]);
}
}


int inserAtPos(int la[], int n, int item, int k){
int i, w,c;
for(i=1;i>0;i++){
if(contains(la,item,n)==1){
printf("The number you want to insert is already in the list. Enter another no.: ");
scanf("%d", &item);
}

else{

c=n;
for(w=k; w<(n+1); w++){
la[c]=la[c-1];
c--;
}
la[k-1]=item;
break;
}
}

return 1;
}


int contains(int la[], int item, int n){
int i, found=0;
for(i=0;i<n;i++){
if(la[i]==item){
found=1;
break;
}}
return found;
}


void insertFront(int la[], int n,int item ){
int i, w, c;
for(i=1;i>0;i++){
if(contains(la,item,n)==1){
printf("The number you want to insert is already in the list. Enter another no.: ");
scanf("%d", &item);
}
else{
c=n;
for(w=(n+1); w>1; w--){
la[c]=la[c-1];
c--;
}
la[0]=item;
break;
}
}
}


void removeAtPos(int la[], int n, int index){
int d, i;
i=index;
for(d=(i-1);d<n-1;d++){
la[index-1]=la[index];
index++;
}
}


int removeItem(int la[], int n, int item){
int d, c, f, remove=1;

if(contains(la,item,n)==0){
remove=-1;
}
else{
for(d=0;d<n;d++){
if(item==la[d]){
c=d;
for(f=(c);f<n-1;f++){
la[d]=la[d+1];
d++;
}
break;
}
}}
return remove;
}


int removeFront(int la[], int n){
int i;
for(i=0;i<n;i++){
la[i]=la[i+1];
}
return 1;
}


void locateIndex(int la[],int n, int item){
int d, i, g, f;
while(f=1){
for(i=0;i<n;i++){
if(item==la[i])
g=1;
f=2;
}
if(g==1){
for(d=0;d<n;d++){
if(item==la[d]){
printf("The item is at position %d.", d+1);
}
}
break;
}
else{
printf("Item is not in the list.");
printf("\nEnter another number: ");
scanf("%d", &item);
f=1;
}}
}


void sortAscending(int la[], int n){
int i, temp, b;
for(i=0;i<n;i++){
for(b=0;b<(n-(i+1));b++){
if (la[b]>la[b+1]){
temp=la[b];
la[b]=la[b+1];
la[b+1]=temp;
}}}
printf("Sorting successful!");
printf("\nARRAY list in ASCENDING order : ");
printArray(la, n);
}


void sortDescending(int la[],int n){
int i, temp, b;
for(i=0;i<n;i++){
for(b=0;b<(n-(i+1));b++){
if (la[b]<la[b+1]){
temp=la[b];
la[b]=la[b+1];
la[b+1]=temp;
}}}
printf("\nARRAY list in DESCENDING order : ");
printArray(la, n);
}


void locateItem(int la[],int n, int item){
int i;

for(i=0;i<n;i++){
if(item==la[i]){
printf("Its in the list!");
break;
}
else{
printf("Not in the list!");
break;
}
}
}

Multi Array

Share

#include <stdio.h>
#define SIZE 15

void main(){

int i, h, ar[SIZE][SIZE], row, col, p1, p2;


printf("Enter how many rows: ");
scanf("%d", &row);
printf("Enter how many columns: ");
scanf("%d", &col);
printf("Enter the numbers you want to put in the array: ");


for (i=0;i<col;i++){

for (h=0;h<row;h++){
scanf("%d", &ar[h][i]);

}}

for (p1=0;p1<col;p1++){
printf("column %d: ", p1+1);
for (p2=0;p2<row;p2++){
printf(" %d", ar[p2][p1]);
}
printf("\n");
}
}

Simple Calculator

Share

void box(){

printf("\n[A] Add");
printf("\n[B] Subtract");
printf("\n[C] Multiply");
printf("\n[D] Divide");
printf(" ");
printf("\nEnter what you want to execute: ");

}

void box2(){

printf("\n");
printf("\nC - reset values and input again");
printf("\nY - Continue and apply another operation to the answer");
printf("\nX - Exit");
printf("\nEnter you choice: ");

}


float add(float x,float y){
return x+y;
}

float sub(float x,float y){
return x-y;
}


float multi(float x,float y){
return x*y;
}

float dev(float x,float y){
return x/y;
}

Sets 0.1

Share

void unionof(int result[], int *count, int set1[], int count1, int set2[], int count2){

int i=0, h=0;
for(i=0;i<count1;i++){
result[(*count)++]=set1[i];
}
for(h=0;h<count2;h++){
if(contains(count1, set1, count2, set2[h])==0){
result[(*count)++]=set2[h];
}}
}


int contains(int count1, int set1[], int count2, int set2[]){

int i, found=0;

for(i=0;i<count1;i++){
if(set1[i]==set2){
found=1;
break;
}
}
return found;
}

void display(int result[],int count){
int i;
printf("{");
for(i=0;i<count;i++)
printf("%d,",result[i]);
printf("\b}\n");
}

Conversion Dec-Octal (function)

Share






void toOctal(int num){

int i, temp, temp2, q, ar[100], count=0, h;

for (i=0;num>0;i++){
q=num;
num=num/8;
temp=num;
temp2=temp*8;
temp=q-temp2;

count++;
ar[i]=temp;
}

for(h=0;h<count;h++){
printf("%d", ar[count-1-h]);
}
}

int toDecimal(char *num){

int b, k, m, n;
int len, sum = 0;

len = strlen(num) - 1;
for(k = 0; k <= len; k++)
{
n = (num[k] - '0');
if ((n > 8) || (n < 0))
{
printf("\n\n ERROR! Octal has only 0 to 7!\n");
return (0);
}
for(b = 1, m = len; m > k; m--)
{
b *= 8;
}
sum = sum + n * b;
}
return(sum);
}