Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ejsimprimirmas

Descargar como docx, pdf o txt
Descargar como docx, pdf o txt
Está en la página 1de 53

#include <stdio.

h>
#include <string.h>
#include <math.h>
#include <ctype.h>

//EJ1
void tostring(char [], int);
int toint(char []);
//EJ2
void sumaNumerosdeUnTexto();
//EJ3
void ultimaLetraDelTexto();
//EJ4
void MasLargaYCorta();
//EJ5
void Print_BinCoeff(int x);
//EJ6
int matrizSimetrica();
//EJ7
int esInvertible();
//EJ8
int arrayReverso();
//EJ9

//EJ10

//EJ11

int main(int argc, const char * argv[]) {

return 0;
}
//EJERCICIO 1- STRING(CHAR) TO INT Y VICEVERSA

void tostring(char str[], int num)


{
int i, rem, len = 0, n;

n = num;
while (n != 0)
{
len++;
n /= 10;
}
for (i = 0; i < len; i++)
{
rem = num % 10;
num = num / 10;
str[len - (i + 1)] = rem + '0';
}
str[len] = '\0';

/*esto iria en el main del ejercicio para imprimir char o int:


char str[10];
int num, result;

printf("Enter a number: ");


scanf("%d", &num);
tostring(str, num);
printf("Number converted to string: %s\n", str);*/

int toint(char str[]){


int len = strlen(str);
int i, num = 0;

for (i = 0; i < len; i++)


{
num = num + ((str[len - (i + 1)] - '0') * pow(10, i));
}
return num;
}

//EJERCICIO 2 -UN TEXTO CON NUMEROS Y LETRAS


HACER LA SUMA DE TODOS SUS NUMEROS

void sumaNumerosdeUnTexto(char string[]){


int count, nc = 0, sum = 0;

printf("Enter the string containing both digits and alphabet \n");


scanf("%s", string);
for (count = 0; string[count] != '\0'; count++)
{
if ((string[count] >= '0') && (string[count] <= '9'))
{
nc += 1;
sum += (string[count] - '0');
}
}
printf("NO. of Digits in the string = %d\n", nc);
printf("Sum of all digits = %d\n", sum);

//este ejercicio es como si estuviera en el main, habria que


adaptarlo para ponerlo en una funcion void

}
//EJERCICIO 3-IMPRIME LAS PALABRAS DE UN TEXTO
QUE ACABEN EN LA LETRA S

void ultimaLetraDelTexto(char str[]){

int i, t, j, len;

printf("Enter a string : ");


scanf("%[^\n]s", str);

len = strlen(str);

str[len] = ' ';

for (t = 0, i = 0; i < strlen(str); i++)


{
if ((str[i] == ' ') && (str[i - 1] == 's'))
{
for (j = t; j < i; j++)
printf("%c", str[j]);
t = i + 1;
printf("\n");
}
else
{
if (str[i] == ' ')
{
t = i + 1;
}
}
}
}

//EJERCICIO 4-PALABRA MAS LARGA Y MAS CORTA


void MasLargaYCorta(){
char string[100], word[20], max[20], min[20], c;
int i = 0, j = 0, flag = 0;
printf("Enter string: ");
i = 0;
do
{
fflush(stdin);
c = getchar();
string[i++] = c;

} while (c != '\n');
string[i - 1] = '\0';
for (i = 0; i < strlen(string); i++)
{
while (i < strlen(string) && !isspace(string[i]) &&
isalnum(string[i]))
{
word[j++] = string[i++];
}
if (j != 0)
{
word[j] = '\0';
if (!flag)
{
flag = !flag;
strcpy(max, word);
strcpy(min, word);
}
if (strlen(word) > strlen(max))
{
strcpy(max, word);
}
if (strlen(word) < strlen(min))
{
strcpy(min, word);
}
j = 0;
}
}
printf("The largest word is '%s' and smallest word is '%s' in
'%s'.\n", max, min, string);
}

//EJERCICIO 5-TRIANGULO DE PASCAL

/*esto es lo que estaria en el main llamando a la funcion void


tambien:

int main(){
int rows;
printf("Enter the Number of Rows in the Pascal Triangle::\t");
scanf("%d",&rows);
for(int i=0;i<rows;i++)//loop to iterate rows from 1 to rows
{
for (int k = rows - i-1 ; k > 0; k--)
{
//Prints spaces as many as number of rows in the triangle-
current number of row
printf(" ");
}
Print_BinCoeff(i);//function to print the binomial
coefficients.
}
return 0;
}
*/

void Print_BinCoeff(int x){


int temp=x;
int answer=x;
int c=2;
for(int i=0;i<=x;i++)
{
if(i==0 || i==x)
{
printf("%-5d",1);
}
else
{
printf("%-5d",answer);
//It's a length specifier, so it specifies the slot in which the
number can be adjusted.
answer=answer*(temp-1);
answer=answer/c;
temp--;
c++;
}
}
printf("\n");
}
//otra forma de hacerlo

/*
int main(){
int array[15][15], i, j, rows, num = 25, k;

printf("\n Enter the number of rows:");


scanf("%d", &rows);
for (i = 0; i < rows; i++)
{
for (k = num - 2 * i; k >= 0; k--)
printf(" ");
for (j = 0; j <= i; j++)
{
if (j == 0 || i == j)
{
array[i][j] = 1;
}
else
{
array[i][j] = array[i - 1][j - 1] + array[i - 1][j];
}
printf("%4d", array[i][j]);
}
printf("\n");
}
}

Y OTRA FORMA USANDO SOLO 1 DIMENSION ARRAY


int main(){
int array[30], temp[30], i, j, k, l, num; //using 2 arrays

printf("Enter the number of lines to be printed: ");


scanf("%d", &num);
temp[0] = 1;
array[0] = 1;
for (j = 0; j < num; j++)
printf(" ");
printf(" 1\n");
for (i = 1; i < num; i++)
{
for (j = 0; j < i; j++)
printf(" ");
for (k = 1; k < num; k++)
{
array[k] = temp[k - 1] + temp[k];
}
array[i] = 1;
for (l = 0; l <= i; l++)
{
printf("%3d", array[l]);
temp[l] = array[l];
}
printf("\n");
}
}

*/
//EJERCICIO 6_COMPROBAR SI UNA MATRIZ ES
SIMETRICA A OTRA

int matrizSimetrica(){
int i, j, row, col, count = 0;

printf("Please Enter Number of rows and columns: ");


scanf("%d %d", &i, &j);

if(i!=j)
{
printf("Rows not equal to columns. Therefore Non-
Symmetric Matrix.");
return 0;
}

//Initialize 2d-array of size i,j.


int a[i][j], b[i][j];

printf("\nEnter the Matrix Elements \n");


for(row = 0; row < i; row++)
{
for(col = 0;col < j;col++)
{
scanf("%d", &a[row][col]);
}
}

//Transpose of matrix
for(row = 0; row < i; row++)
{
for(col = 0;col < j; col++)
{
b[col][row] = a[row][col];
}
}

//Check if matrix a equals to matrix b or not.


for(row = 0; row < i; row++)
{
for(col = 0; col < j; col++)
{
if(a[row][col] != b[row][col])
{
count++;
break;
}
}
}
if(count == 0)
{
printf("\nThe given Matrix is a Symmetric Matrix ");
}
else
{
printf("\nThe given Matrix is Not a Symmetric Matrix ");
}

return 0;
}

//EJERCICIO 7-COMPROBAR SI UNA MATRIZ ES


INVERTIBLE

int esInvertible(){

int a[3][3], i, j;
long determinant;
printf("Enter the 9 elements of matrix: ");
for(i = 0 ;i < 3;i++)
for(j = 0;j < 3;j++)
scanf("%d", &a[i][j]);

printf("\nThe matrix is\n");


for(i = 0;i < 3; i++){
printf("\n");
for(j = 0;j < 3; j++)
printf("%d\t", a[i][j]);
}
determinant = a[0][0] * ((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -
a[0][1] * (a[1][0]
* a[2][2] - a[2][0] * a[1][2]) + a[0][2] * (a[1][0] * a[2][1] - a[2]
[0] * a[1][1]);
if ( determinant == 0)
printf("\nMatrix is NOT invertible");
else
printf("\nThe given matrix has an inverse!!!");
return 0;
}

//EJERCICIO 8-ARRAY REVERSO

int arrayReverso(){
int size;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter Array Elements: ");
int arr[size];

//Input array elements


for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
printf("Entered Array is: ");
for(int i=0;i<size;i++)
printf("%d ",arr[i]);

//Start points at the first element and end points at the last
element
int start=0,end=size-1;
while(start<end)
{
//Swapping elements
int temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;

//Incrementing start and decrementing end


start++;
end--;
}

//Printing reversed array


printf("\nReversed array is: ");
for(int i=0;i<size;i++)
printf("%d ",arr[i]);
return 0;
}

//EJERCICIO 9_DECODIFICAR MENSAJE


/*
printf("\nDecoded message is: ");
mul(inv_key, result, decode);
for (i = 0; i < 10; i++)
{
for ( j = 0; j < 3; j++)
{
if ( ((decode[j][i]+96)) >= 97 && ((decode[j][i]+96) <=
123))
printf("%c", (decode[j][i] + 96) );
else if ( decode[j][i] == 32)
printf(" ");
else
return;
}
}
return 0;
}

ELIMINAR UN ELEMENTO DE UN ARRAY

int main(void)
{
int i, n, index, arr[10];
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: \n");
for (i = 0; i < n; i++)
{
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("Enter the index of the element to be deleted: ");
scanf("%d", &index);
if (index >= n+1)
{
printf (" \n Deletion is not possible in the array.");
}
else
{
for (i = index; i < n - 1; i++)
arr[i] = arr[i + 1];
printf("The array after deleting the element is: ");
for (i = 0; i < n - 1; i++)
printf("%d ", arr[i]);
return 0;
}
}
*/

//QUITAR DUPLICADAS EN UN STRING

/*int main ()
{
char str[100], word[100], twoD[10][30];
int i = 0, j = 0, k = 0, len1 = 0, len2 = 0, l = 0;

printf ("Enter the string\n");


gets (str);

// let us convert the string into 2D array


for (i = 0; str[i] != '\0'; i++)
{
if (str[i] == ' ')
{
twoD[k][j] = '\0';
k ++;
j = 0;
}
else
{
twoD[k][j] = str[i];
j ++;
}
}

twoD[k][j] = '\0';
j = 0;
for (i = 0; i < k; i++)
{
int present = 0;
for (l = 1; l < k + 1; l++)
{
if (twoD[l][j] == '\0' || l == i)
{
continue;
}

if (strcmp (twoD[i], twoD[l]) == 0) {


twoD[l][j] = '\0';
present = present + 1;
}
}
// if (present > 0) | uncomment this `if` block if you
// { | want to remove all the occurrences
// twoD[i][j] = '\0'; | of the words including the word
// } | itself.
}

j = 0;

for (i = 0; i < k + 1; i++)


{
if (twoD[i][j] == '\0')
continue;
else
printf ("%s ", twoD[i]);
}

printf ("\n");

return 0;
}*/
//NUMERO DE CONSONANTES Y VOCALES

/*void main()
{
char sentence[80];
int i, vowels = 0, consonants = 0, special = 0;

printf("Enter a sentence \n");


gets(sentence);
for (i = 0; sentence[i] != '\0'; i++)
{
if ((sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] ==
'i' || sentence[i] == 'o' || sentence[i] == 'u') ||
(sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] ==
'I' || sentence[i] == 'O' || sentence[i] == 'U'))
{
vowels = vowels + 1;
}
else
{
consonants = consonants + 1;
}
if (sentence[i] =='\t' ||sentence[i] =='\0' || sentence[i] ==' ')
{
special = special + 1;
}
}
consonants = consonants - special;
printf("No. of vowels in %s = %d\n", sentence, vowels);
printf("No. of consonants in %s = %d\n", sentence,
consonants);
}
*/
//NUMERO REVERSO Y COMPRUEBA SI ES
PALINDROMO

/*
int main(){
int num, temp, remainder, reverse = 0;

printf("Enter an integer \n");


scanf("%d", &num);
temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Given number is = %d\n", temp);
printf("Its reverse is = %d\n", reverse);
if (temp == reverse)
printf("Number is a palindrome \n");
else
printf("Number is not a palindrome \n");
}
*/

//BINARIO A DECIMAL(SOLO HACER CASO A LA


OPERACION SI ESO)
/*
int main()
{
int num, binary_val, decimal_val = 0, base = 1, rem;

printf("Enter a binary number(1s and 0s) \n");


scanf("%d", &num); (maximo 5 digitos)
binary_val = num;
while (num > 0)
{
rem = num % 10;
decimal_val = decimal_val + rem * base;
num = num / 10 ;
base = base * 2;
}
printf("The Binary number is = %d \n", binary_val);
printf("Its decimal equivalent is = %d \n", decimal_val);
}*/

//DECIMAL A BINARIO

/*
long dec_to_bin(long n){
long bin = 0;
long dec = n;
long i = 1;

while (dec > 0) {


bin += (dec % 2) * i;
dec /= 2;
i *= 10;
}

return bin;
}

int main(void){
long n;
printf("Enter a decimal number: ");
scanf("%ld", &n);
printf("Binary equivalent of %ld = %ld\n", n, dec_to_bin(n));
return 0;
}
*/

//BINARIO A OCTAL

/*
int main(){
long int binarynum, octalnum = 0, j = 1, remainder;

printf("Enter the value for binary number: ");


scanf("%ld", &binarynum);
while (binarynum != 0)
{
remainder = binarynum % 10;
octalnum = octalnum + remainder * j;
j = j * 2;
binarynum = binarynum / 10;
}
printf("Equivalent octal value: %lo", octalnum);
return 0;
}*/

//OCTAL A DECIMAL

/*
int main(){

long int octal, decimal = 0;


int i = 0;

printf("Enter any octal number: ");


scanf("%ld", &octal);
while (octal != 0)
{
decimal = decimal +(octal % 10)* pow(8, i++);
octal = octal / 10;
}
printf("Equivalent decimal value: %ld",decimal);
return 0;
}*/

//BINARIO A HEXADECIMAL

/*
int main(){
long int binaryval, hexadecimalval = 0, i = 1, remainder;

printf("Enter the binary number: ");


scanf("%ld", &binaryval);
while (binaryval != 0)
{
remainder = binaryval % 10;
hexadecimalval = hexadecimalval + remainder * i;
i = i * 2;
binaryval = binaryval / 10;
}
printf("Equivalent hexadecimal value: %lX", hexadecimalval);
return 0;
}*/

//DECIMAL A HEXADECIMAL

/*

int main(){
long decimalnum, quotient, remainder;
int i, j = 0;
char hexadecimalnum[100];

printf("Enter decimal number: ");


scanf("%ld", &decimalnum);

quotient = decimalnum;

while (quotient != 0)
{
remainder = quotient % 16;
if (remainder < 10)
hexadecimalnum[j++] = 48 + remainder;
else
hexadecimalnum[j++] = 55 + remainder;
quotient = quotient / 16;
}

// display integer into character


for (i = j; i >= 0; i--)
printf("%c", hexadecimalnum[i]);
return 0;
}*/

//DECIMAL A BINARIO Y CONTAR EL NUMERO DE UNOS

/*
int main(){
long num, decimal_num, remainder, base = 1, binary = 0,
no_of_1s = 0;

printf("Enter a decimal integer \n");


scanf("%ld", &num);
decimal_num = num;
while (num > 0)
{
remainder = num % 2;

if (remainder == 1){
no_of_1s++;
}
binary = binary + remainder * base;
num = num / 2;
base = base * 10;
}
printf("Input number is = %d\n", decimal_num);
printf("Its binary equivalent is = %ld\n", binary);
printf("No.of 1's in the binary number is = %d\n", no_of_1s);
}*/

//NUMEROS A NUMEROS ROMANOS

/*
void predigit(char num1, char num2);
void postdigit(char c, int n);

char romanval[1000];
int i = 0;
int main(){
int j;
long number;

printf("Enter the number: ");


scanf("%d", &number);
if (number <= 0){
printf("Invalid number");
return 0;
}
while (number != 0){
if (number >= 1000){
postdigit('M', number / 1000);
number = number - (number / 1000) * 1000;
}
else if (number >= 500){
if (number < (500 + 4 * 100)){
postdigit('D', number / 500);
number = number - (number / 500) * 500;
}
else{
predigit('C','M');
number = number - (1000-100);
}
}
else if (number >= 100){
if (number < (100 + 3 * 100)){
postdigit('C', number / 100);
number = number - (number / 100) * 100;
}
else{
predigit('L', 'D');
number = number - (500 - 100);
}
}
else if (number >= 50 ){
if (number < (50 + 4 * 10)){
postdigit('L', number / 50);
number = number - (number / 50) * 50;
}
else{
predigit('X','C');
number = number - (100-10);
}
}
else if (number >= 10){
if (number < (10 + 3 * 10)){
postdigit('X', number / 10);
number = number - (number / 10) * 10;
}
else{
predigit('X','L');
number = number - (50 - 10);
}
}
else if (number >= 5){
if (number < (5 + 4 * 1)){
postdigit('V', number / 5);
number = number - (number / 5) * 5;
}
else{
predigit('I', 'X');
number = number - (10 - 1);
}
}
else if (number >= 1){
if (number < 4){
postdigit('I', number / 1);
number = number - (number / 1) * 1;
}
else{
predigit('I', 'V');
number = number - (5 - 1);
}
}
}
printf("Roman number is: ");
for(j = 0; j < i; j++)
printf("%c", romanval[j]);
return 0;
}

void predigit(char num1, char num2){


romanval[i++] = num1;
romanval[i++] = num2;
}

void postdigit(char c, int n){


int j;
for (j = 0; j < n; j++)
romanval[i++] = c;
}

//CONVERSION TEMPERATURAS
int main(){
float Celsius, Kelvin, Fahrenhiet;
int choice;
printf("Choose the unit you want to convert: \n");
printf("1.Kelvin\n");
printf("2.Celsius\n");
printf("3.Fahrenhiet \n");
scanf("%d", &choice);
switch (choice){
case 1:
printf("Enter the temperature in kelvin: ");
scanf("%f",&Kelvin);
Celsius = (Kelvin - 273.15);
Fahrenhiet = 1.8 *(Kelvin -273.15) + 32.0;
printf("In Celsius the value is:\t %f \n",Celsius);
printf("In Fahrenhiet the value is:\t %f",Fahrenhiet);
break;
case 2:
printf("Enter the temperature in Celsius: ");
scanf("%f",&Celsius);
Kelvin = (Celsius + 273.15);
Fahrenhiet = (Celsius * 1.8) + 32.0;
printf("In kelvin the value is : %f \n",Kelvin);
printf("In Fahrenhiet the value is: %f",Fahrenhiet);
break;
case 3:
printf("Enter the temperature in fahrenhite: ");
scanf("%f",&Fahrenhiet);
Kelvin = (Fahrenhiet - 32.0) * 5/9 +273.15;
Celsius = (Fahrenheit - 32.0) * 5/9;
printf("In Celsius the value is :\t %f \n",Celsius);
printf("In Kevlin the value is :\t %f \n",Kelvin);
break;
default:
printf("Please! enter a valid value \n");
break;
}
printf("\n\n");
return 0;
}*/

//ROMANO A DECIMAL

/*
int digit(char);

int main(){

char roman_Number[1000];
int i=0;
long int number =0;

printf("Enter any roman number (Valid digits are I, V, X, L, C,


D, M): \n");
scanf("%s",roman_Number);

while(roman_Number[i]){

if(digit(roman_Number[i]) < 0){


printf("Invalid roman digit : %c",roman_Number[i]);
return 0;
}

if((strlen(roman_Number) -i) > 2){


if(digit(roman_Number[i]) < digit(roman_Number[i+2]))
{
printf("Invalid roman number");
return 0;
}
}

if(digit(roman_Number[i]) >= digit(roman_Number[i+1]))


number = number + digit(roman_Number[i]);
else{
number = number + (digit(roman_Number[i+1]) -
digit(roman_Number[i]));
i++;
}
i++;
}

printf("Its decimal value is : %ld",number);

return 0;

int digit(char c){

int value=0;

switch(c){
case 'I': value = 1; break;
case 'V': value = 5; break;
case 'X': value = 10; break;
case 'L': value = 50; break;
case 'C': value = 100; break;
case 'D': value = 500; break;
case 'M': value = 1000; break;
case '\0': value = 0; break;
default: value = -1;
}

return value;
}*/
//MULTIPLICACION DE BINARIOS

/*
int binaryproduct(int, int);

int main(){

long binary1, binary2, multiply = 0;


int digit, factor = 1;

printf("Enter the first binary number: ");


scanf("%ld", &binary1);
printf("Enter the second binary number: ");
scanf("%ld", &binary2);
while (binary2 != 0){
digit = binary2 % 10;
if (digit == 1){
binary1 = binary1 * factor;
multiply = binaryproduct(binary1, multiply);
}
else
binary1 = binary1 * factor;
binary2 = binary2 / 10;
factor = 10;
}
printf("Product of two binary numbers: %ld", multiply);
return 0;
}

int binaryproduct(int binary1, int binary2){


int i = 0, remainder = 0, sum[20];
int binaryprod = 0;

while (binary1 != 0 || binary2 != 0){


sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (remainder != 0)
sum[i++] = remainder;
--i;
while (i >= 0)
binaryprod = binaryprod * 10 + sum[i--];
return binaryprod;
}

//NUMERO DE PALABRAS

void main(){
char s[200];
int count = 0, i;

printf("Enter the string:\n");


scanf("%[^\n]s", s);
for (i = 0;s[i] != '\0';i++){
if (s[i] == ' ' && s[i+1] != ' ')
count++;
}
printf("Number of words in given string are: %d\n", count +
1);}

//NUMEROS PARES E IMPARES EN UN ARRAY

void main(){
int n;
printf("Enter number of elements in the array: ");
scanf("%d", &n);

int arr[n];

//Take n elements as input from the user


printf("Enter %d elements in the array: ",n);
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}

//Print all the even numbers


printf("Even numbers in the array are: ");
for(int i=0;i<n;i++){
if(arr[i]%2==0)
printf("%d ", arr[i]);
}

//print all the odd numbers


printf("\nOdd numbers in the array are: ");
for(int i=0;i<n;i++){
if(arr[i]%2==1)
printf("%d ", arr[i]);
}

//INTERCAMBIAR LAS DIAGONALES DE UNA MATRIZ

int main (){

static int array[10][10];


int i, j, m, n, a;

printf("Enter the order of the matix \n");


scanf("%d %d", &m, &n);

if (m == n){
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i){
for (j = 0; j < n; ++j){
scanf("%dx%d", &array[i][j]);
}
}

printf("The given matrix is \n");


for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j){
printf(" %d", array[i][j]);
}
printf("\n");
}

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


a = array[i][i];
array[i][i] = array[i][m - i - 1];
array[i][m - i - 1] = a;
}

printf("The matrix after changing the \n");


printf("main diagonal & secondary diagonal\n");
for (i = 0; i < m; ++i){
for (j = 0; j < n; ++j){
printf(" %d", array[i][j]);
}
printf("\n");
}
}
else{
printf("The given order is not square matrix\n");

}*/

//ELIMINAR DUPLICADOS EN UN ARRAY

/*int main(){
int n, count = 0;
printf("Enter number of elements in the array: ");
scanf("%d", &n);
int arr[n], temp[n];
if(n==0){
printf("No element inside the array.");
exit(0);
}
printf("Enter elements in the array: ");
for (int i = 0; i < n; i++){
scanf("%d", &arr[i]);
}

printf("\nArray Before Removing Duplicates: ");


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

// To store unique elements in temp after removing the


duplicate elements

for (int i = 0; i < n; i++){


int j;
for (j = 0; j < count; j++){
if (arr[i] == temp[j])
break;
}
if (j == count){
temp[count] = arr[i];
count++;
}
}

printf("\nArray After Removing Duplicates: ");


for (int i = 0; i < count; i++)
printf("%d ", temp[i]);

return 0;
}
//MODA DE UN ARRAY

int main(){
int i, j, a[20] = {0}, sum = 0, n, t, b[20] = {0}, k = 0, c = 1,
max = 0, mode;
float x = 0.0, y = 0.0;
printf("\nEnter the limit\n");
scanf("%d", &n);
printf("Enter the set of numbers\n");
for (i = 0; i < n; i++){
scanf("%d", &a[i]);
}
for (i = 0; i < n - 1; i++){
mode = 0;
for (j = i + 1; j < n; j++){
if (a[i] == a[j]){
mode++;
}
}
if ((mode > max) && (mode != 0)) {
k = 0;
max = mode;
b[k] = a[i];
k++;
}
else if (mode == max) {
b[k] = a[i];
k++;
}
}
for (i = 0; i < n; i++){
if (a[i] == b[i])
c++;
}
if (c == n)
printf("\nThere is no mode");
else{
printf("\nMode\t= ");
for (i = 0; i < k; i++)
printf("%d ",b[i]);
}
printf("\n");
return 0;
}

//TEXTO A ASCII DE CADA LETRA


int main(){

char string[20];
int n, count = 0;

printf("Enter the no of characters present in an array \n ");


scanf("%d", &n);

printf(" Enter the string of %d characters \n" , n);


scanf("%s", string);

while (count < n){


printf(" %c = %d\n", string[count], string[count] );
++ count ;
}

}*/

//DADO UN TEXTO ENCONTRAR LA POSICION DE LA


PRIMERA Y ULTIMA APARICION DEL CARACTER
INGRESADO
/*
int main(){
int i, count = 0, pos1, pos2;
char str[50], key, a[10];

printf("enter the string\n");


scanf(" %[^\n]s", str);
printf("enter character to be searched\n");
scanf(" %c", &key);
for (i = 0;i <= strlen(str);i++){
if (key == str[i]){
count++;
if (count == 1){
pos1 = i;
pos2 = i;
printf("%d\n", pos1 + 1);
}
else{
pos2 = i;
}
}
}
printf("%d\n", pos2 + 1);
}*/

//TEXTO AL REVES

int main(){
char str1[100], *ptr;
int len1 = 0, i;
char ch;
printf("Enter the string:\n");
scanf("%[^\n]s", str1);
ptr = str1;
len1 = strlen(str1);
printf("Using iteration:\n");
for (i = len1 - 1; i >= 0;i--){
ch = str1[i];
printf("%c", ch);
}*/

//PRIMERA VOCAL DE UN TEXTO

/*
char caps_check(char *);

int main()
{
char string[20], letter;

printf("Enter a string to find it's first capital letter: ");


scanf("%s", string);
letter = caps_check(string);
if (letter == 0){
printf("No capital letter is present in %s.\n", string);
}
else{
printf("The first capital letter in %s is %c.\n", string, letter);
}
return 0;
}
char caps_check(char *string)
{
int i = 0;
while (string[i] != '\0')
{
if (isupper(string[i]))
{
return string[i];
}
i++;
}
return 0;
}*/

//ENCONTRAR LA POSICION DE STRINGS DE 1s BIT EN


UN NUMERO
/*
int main(){
int n, len, pos = 0, i = 0, count = 0;

printf("**Finding the position of 1-bits in a number for given


length**\n");
printf("enter a number\n");
scanf("%d", &n);
printf("enter the length\n");
scanf("%d", &len);
while (i <= 32){
if ((n & 1) == 1) { //checking
count++;
pos = i;
if (count == len){
break;
}
}
if ((n & 1) == 0){
count = 0;
}
n = n>>1;
i++;
}
printf("the position of 1 in the string : %d\n", pos);
}*/

//INGRESAS UN BYTE(8 BITS:10001100) Y LA POSICION


DESDE A HASTA B QUE QUIERES QUE SE IMPRIMAN
LOS BITS (0 a 4 por ejemplo) e imprime 10001

/*
int main (){
int a = 0, b = 0, temp = 0, i = 0, countCast = 0;

char BYTE_HERE[8];
int FULL_BYTE[8];

printf ("Enter the BYTE: \n");


gets(BYTE_HERE);

if (strlen (BYTE_HERE) < 8){


printf ("Enter a full 8-bit value.\n");
return 0;
}

printf ("\nEnter the positions a and b : \n");


scanf ("%d %d", &a, &b);

// copy from character array to integer array


for (i = 0; i < 8; i++){
// convert the character to integer
FULL_BYTE[i] = BYTE_HERE[i] - '0';
}
// just print the bits
for (i = a; i <= b; i++){
printf ("%d ", FULL_BYTE[i]);
}
printf("\nBits between positions a and b are:\n");

return 0;
}

//COMPRUEBA SI TODOS LOS BITS DEL NUMERO LEIDO


POR PANTALLA SON 1

/*
int main (){
int num = 0, count = 0, n = 0, i = 0;

printf ("\nEnter the number : ");


scanf ("%d", &num);
n = num;
if (num == 0){
printf ("\nFALSE : ALL BITS ARE NOT SET\n");
exit (0);//poner una variable como i=-1 y cuando sea, dejar
de ejecutar
}
while (n)
{
count ++;
n = n >> 1;
}
for (i = 0; i < count; i++){
if (((num >> i) & 1) == 1){
continue;
}
else{
printf ("\nFALSE : ALL BITS ARE NOT SET\n");
exit (0);//cambiar esto
}
}
printf ("\nTRUE : ALL BITS ARE SET\n");
return 0;
}*/

//ORDENAR ARRAY DE NUMEROS DE MENOR A MAYOR

/*int main(){
int array[MAXSIZE];
int i, j, num, temp;

printf("Enter the value of num \n");


scanf("%d", &num);
printf("Enter the elements one by one \n");

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


scanf("%d", &array[i]);
}
printf("Input array is \n");

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


printf("%d\n", array[i]);
}

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


for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array is...\n");

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


printf("%d\n", array[i]);
}
}*/

//ORDENAR,BORRAR ESPACIOS Y CARACTERES


REPETIDOS DE UN TEXTO

/*int main(){
char string[SIZE], string1[SIZE], string2[SIZE];
int i, j = 0, a = 0, temp, len = 0, len1 = 0, k = 0;

printf("\nEnter a string:");
scanf("%[^\n]s", string1);

for (i = 0;string1[i] != '\0';i++){


if (string1[i] == ' '){
continue;
}
string[j++] = string1[i];
}

for (i = 0;string[i] != '\0';i++)//ordenar texto(string)


{
for (j = i + 1;string[j] != '\0';j++)
{
if (string[i] > string[j])
{
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
}
}
string[i] = '\0';
len = strlen(string);

for (i = 0;string[i] != '\0';i++)//borrar repetidos{


if (string[i] == string[i + 1] && string[i + 1] != '\0')
{
k++;
continue;
}
string2[a++] = string[i];
string[a] = '\0';
}
len1 = len - k;
printf("The sorted string is:");
for (temp = 0;temp < len1;temp++)
{
printf("%c", string2[temp]);
}
}*/

//ORDENAR UNA MATRIZ MxN: FILAS EN ASCENDIENTE


Y COLUMNAS EN DESCENDIENTE

/*
int main(){
int array1[10][10], array2[10][10];
int i, j, k, a, m, n;
printf("Enter the order of the matrix \n");
scanf("%d %d", &m, &n);
printf("Enter co-efficients of the matrix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array1[i][j]);
array2[i][j] = array1[i][j];
}
}
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array1[i][j]);
}
printf("\n");
}
printf("After arranging rows in ascending order\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
for (k =(j + 1); k < n; ++k)
{
if (array1[i][j] > array1[i][k])
{
a = array1[i][j];
array1[i][j] = array1[i][k];
array1[i][k] = a;
}
}
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array1[i][j]);
}
printf("\n");
}
printf("After arranging the columns in descending order \n");
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
for (k = i + 1; k < m; ++k)
{
if (array2[i][j] < array2[k][j])
{
a = array2[i][j];
array2[i][j] = array2[k][j];
array2[k][j] = a;
}
}
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array2[i][j]);
}
printf("\n");
}
}*/

//PATRON DE DIAMANTE CON ASTERISCOS


int main(void){
int number;
printf("Enter the number: ");
scanf("%d", &number);
diamondPattern(number);
}

void diamondPattern(int num){

int str = 1;
while(str <= num){
int i = 1;
while(i ++ <= (num - str) * 2 + 1)
printf(" ");
i = 0;
while(i ++ < 2 * str - 1)
printf("* ");
str ++;
printf("\n");
}
str = num - 1;
while(str != 0)
{
int i = 1;
while(i ++ <= (num - str) * 2 + 1)
printf(" ");
i = 0;
while(i ++ < 2 * str - 1)
printf("* ");
str --;
printf("\n");
}
}
//IMPRIMIR ESTE PATRON(FLOYD TRIANGLE)
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28

int main(){
int i = 1, j = 0, rows = 7;
int num = 1;
while (i <= rows)
{
while (j <= i - 1)
{
printf("%d ", num);
j++;
num++;
}
j = 0;
i++;
printf("\n");
}
return 0;
}*/

//LEE ECUACION POLINOMICA Y EL VALOR DE X Y


RESUELVE

/*
int main(){
int array[MAXSIZE];
int i, num, power;
float x, polySum;
printf("Enter the order of the polynomial \n");
scanf("%d", &num);
printf("Enter the value of x \n");
scanf("%f", &x);

printf("Enter %d coefficients \n", num + 1);


for (i = 0; i <= num; i++)
{
scanf("%d", &array[i]);
}
polySum = array[0];
for (i = 1; i <= num; i++)
{
polySum = polySum * x + array[i];
}
power = num;

printf("Given polynomial is: \n");


for (i = 0; i <= num; i++)
{
if (power < 0)
{
break;
}

if (array[i] > 0)
printf(" + ");
else if (array[i] < 0)
printf(" - ");
else
printf(" ");
printf("%dx^%d ", abs(array[i]), power--);
}
printf("\n Sum of the polynomial = %6.2f \n", polySum);
}*/
//STRING PALINDROMO

/*
void main(){
char string[25], reverse_string[25] = {'\0'};
int i, length = 0, flag = 0;

printf("Enter a string \n");


gets(string);

for (i = 0; string[i] != '\0'; i++)


{
length++;
}
printf("The length of the string '%s' = %d\n", string, length);
for (i = length - 1; i >= 0 ; i--)
{
reverse_string[length - i - 1] = string[i];
}

for (flag = 1, i = 0; i < length ; i++)


{
if (reverse_string[i] != string[i])
flag = 0;
}
if (flag == 1)
printf ("%s is a palindrome \n", string);
else
printf("%s is not a palindrome \n", string);
}

/*CONTADOR DE BITS NECESARIOS PARA "FLIP"


INTEGER X A INTEGER Y
void main(){
int n, m, i, count = 0, a, b;

printf("Enter the number\n");


scanf("%d", &n);
printf("Enter another number\n");
scanf("%d", &m);
for (i = NUM_BITS_INT-1;i >= 0;i--)
{
a = (n >> i)& 1;
b = (m >> i)& 1;
if (a != b)
count++;
}
printf("flip count = %d\n", count);
}*/

/*

//ENUNCIADO EJERCICIO

Desarrollar un programa con las siguientes opciones:

a) Introducir un valor entero impar comprendido entre 1 y 19

b) Calcular la serie numérica 1 + 3 + 5 + ··· + n

c) Calcular 1 * 3 * 5 * ··· * n

d) Salir del programa.


int valor[NMAX]; //Declaración de array como variable global,
realmente solo usaremos 10 elementos
int numeroItems; //Declaración como variable global
int obtenerValorParaCalculo(); void rellenarArray (int numero);
long valorSumatorio (int numeroItemsSum); long valorProducto
(int numeroItemsProd);

int main() {
int valorParaCalculo=0; int opcionUsuario = 1;
while (opcionUsuario!=4) {
printf("\n\n1: Introducir valor\n2: Calcular serie 1+3+5...\
n");
printf("3: Calcular serie 1*3*5...\n4: Salir\n\nElija opcion:
");
scanf("%d", &opcionUsuario);

if (opcionUsuario==1) {
valorParaCalculo = obtenerValorParaCalculo();
rellenarArray (valorParaCalculo);
}

if (opcionUsuario==2) {
if (valorParaCalculo!=0) {
printf("n = %d El valor del sumatorio es: %ld",
valorParaCalculo, valorSumatorio(numeroItems));
} else {
printf("No hay un valor para calculo que permita
obtener sumatorio 1+3+5+...");
}
}

if (opcionUsuario==3) {
if (valorParaCalculo!=0) {
printf("n = %d El valor del producto es: %ld",
valorParaCalculo, valorProducto(numeroItems));
} else {
printf("No hay un valor para calculo que permita
obtener resultado 1*3*5*...");
}
}
}
return 0;
}

int obtenerValorParaCalculo() {
int imparElegido = 0;
do {
printf("\nPor favor introduzca numero entero impar entre 1
y 19: ");
scanf("%d", &imparElegido);
} while (imparElegido<=0 || imparElegido> NMAX ||
imparElegido%2==0);
return imparElegido;
}

void rellenarArray (int numeroElegido) {


int impar = -1; int i=0;
do {
i = i+1;
impar = impar +2;
valor[i] = impar;
} while (impar < numeroElegido);
numeroItems = i;
}

long valorSumatorio (int numeroItemsSum) {


long resultadoSum =0; int i=0;
for (i=1; i<=numeroItemsSum; i++) {
resultadoSum = resultadoSum + valor[i];
}
return resultadoSum; //Devolvemos el valor del sumatorio
}
long valorProducto (int numeroItemsProd) {
int i=0; long resultadoProd = 1;
for (i=1; i<=numeroItemsProd; i++) {
resultadoProd = resultadoProd * valor[i];
}
return resultadoProd; //Devolvemos el valor del producto
}

También podría gustarte