Archivos en la Categoría: introduccion a la informatica para cientificos

Códigos escritos por Santiago Codesido Sánchez

ESCRIBA UN PROGRAMA QUE CALCULE LA LONGITUD DE UNA CADENA DE CARACTERES.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *str;
printf(“Introduzca una cadena de caracteres: “);
scanf(“%s”,str);
printf(“Su longitud es %d\n”,strlen(str));
system(“PAUSE”);
return 0;
}

ESCRIBA UN PROGRAMA QUE CONCATENE EL NOMBRE, LOS APELLIDOS Y LA EDAD DE UNA PERSONA.

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

#include <string.h>

int main(int argc, char *argv[])
{
char nom[128];
char apu[128];
char apd[128];
char eda[128];
char fin[4*128+3];
printf(“Introduzca el nombre: “);
scanf(“%s”,nom);
printf(“Introduzca el primer apellido: “);
scanf(“%s”,apu);
printf(“Introduzca el segundo apellido: “);
scanf(“%s”,apd);
printf(“Introduzca la edad: “);
scanf(“%s”,eda);

strcat(fin,nom);
strcat(fin,” “);
strcat(fin,apu);
strcat(fin,” “);
strcat(fin,apd);
strcat(fin,” “);
strcat(fin,eda);
printf(“%s\n”,fin);
system(“PAUSE”);
return 0;
}

Códigos escritos por Santiago Codesido Sánchez

ESCRIBA UN PROGRAMA QUE CALCULE EL MÓDULO DE UN VECTOR DE NÚMEROS ENTEROS.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
int n;
float sum=0,vec;
printf(“Dimension del vector:”);
scanf(“%d”,&n);
for(int i=0;i<n;i++){
scanf(“%f”,&vec);
sum+=vec*vec;
}
printf(“El modulo es %f”,sqrt(sum));
system(“PAUSE”);
return 0;
}

ESCRIBA UN PROGRAMA QUE CALCULE LA MEDIA DE LOS COMPONENTES DE UN VECTOR DE NÚMEROS ENTEROS.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
int n;
float sum=0,vec;
printf(“Dimension del vector:”);
scanf(“%d”,&n);
for(int i=0;i<n;i++){
scanf(“%f”,&vec);
sum+=vec;
}
printf(“La media es %f”,sum/n);
system(“PAUSE”);
return 0;
}

ESCRIBA UN PROGRAMA QUE DETECTE LA POSICIÓN DEL PRIMER NÚMERO NEGATIVO EN UN VECTOR DE NÚMEROS ENTEROS (POSITIVOS Y NEGATIVOS).

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

int main(int argc, char *argv[])
{
int len;
printf(“Introduzca el tamaño del vector: “);
scanf(“%d”,&len);
int x[len];
printf(“Introduzca los valores:\n”);
for(int i=0;i<len;i++){
scanf(“%d”,x+i);
}
int found=1;
for(int i=0;i<len;i++){
if(*(x+i)<0){
found=*(x+i);
printf(“El primer numero negativo es %d en la posicion %d\n”,found,i+1);
break;
}
}
if(found>0) printf(“No hay numeros negativos\n”);
system(“PAUSE”);
return 0;
}

ESCRIBA UN PROGRAMA QUE INTERCAMBIE EL ELEMENTO DEL VECTOR v QUE OCUPA LA POSICIÓN p, CON EL ÚLTIMO ELEMENTO DEL VECTOR.

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

int main(int argc, char *argv[])
{
int len,pos,temp;
printf(“Introduzca el tamaño del vector: “);
scanf(“%d”,&len);
int x[len];
printf(“Introduzca los valores:\n”);
for(int i=0;i<len;i++){
scanf(“%d”,x+i);
}
do{
printf(“Introduzca la posicion a intercambiar (1-%d):”,len);
scanf(“%d”,&pos);
}while(pos<=0 || pos>len);
temp=*(x+len-1);
*(x+len-1)=*(x+pos-1);
*(x+pos-1)=temp;
for(int i=0;i<len;i++){
printf(“%d”,*(x+i));
}
printf(“\n”);
system(“PAUSE”);
return 0;
}

ESCRIBA UN PROGRAMA QUE TRANSFORME UN VECTOR v DE N ENTEROS DE MANERA QUE EL ELEMENTO k PASE A ALMACENAR LA SUMA DE LOS ELEMENTOS DEL VECTOR DESDE 0 HASTA k. ES DECIR, EL ELEMENTO k ALMACENARÁ v[0] + v[1] + v[2] +…+ v[k].

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int len,read,sum=0;
printf(“Introduzca el tamaño del vector: “);
scanf(“%d”,&len);
int x[len];
printf(“Introduzca los valores:\n”);
for(int i=0;i<len;i++){
scanf(“%d”,&read);
sum+=read;
*(x+i)=sum;
}
printf(“(“);
for(int i=0;i<len-1;i++){
printf(“%d,”,*(x+i));
}
printf(“%d)\n”,*(x+len-1));
system(“PAUSE”);
return 0;
}

Códigos escritos por Santiago Codesido Sánchez

ESCRIBA UN PROGRAMA QUE CALCULE EL FACTORIAL DE UN NÚMERO, PERO UTILIZANDO LA ESTRUCTURA while.

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

int main(int argc, char *argv[])
{
int n,i=1,r=1;
printf(“Introduzca n: “);
scanf(“%d”,&n);
while(i<=n){
r*=i;
i+=1;
}
printf(“%d!=%d\n”,n,r);
system(“pause”);
return 0;
}

ESCRIBA UN PROGRAMA QUE REALICE LAS SIGUIENTES OPERACIONES: SUMA, RESTA, MULTIPLICACIÓN Y DIVISIÓN.

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

int main(int argc, char *argv[])
{
int opt;
float r,m,n;
printf(“Menu:\n\t1:Suma\n\t2:Resta\n\t3:Multiplicación\n\t4:División\n\n”);
scanf(“%d”,&opt);
while(opt<0 || opt>4){
scanf(“%d”,&opt);
}
printf(“Introduzca los números\n”);
scanf(“%f”,&m);
scanf(“%f”,&n);
if(opt==1){
r=m+n;
}else if(opt==2){
r=m-n;
}else if(opt==3){
r=m*n;
}else{
r=m/n;
}
printf(“\n%f\n”,r);
system(“pause”);
return 0;
}

MODIFIQUE EL PROGRAMA DEL EJERCICIO ANTERIOR, AÑADIENDO LA POSIBILIDAD DE CERRARLO.

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

int main(int argc, char *argv[])
{
int opt;
float r,m,n;
while(true){
printf(“Menu:\n\t1:Suma\n\t2:Resta\n\t3:Multiplicación\n\t4:División\n\t5:salir\n\n”);
scanf(“%d”,&opt);
if(opt==5) break;
while(opt<0 || opt>4){
scanf(“%d”,&opt);
}
printf(“Introduzca los números\n”);
scanf(“%f”,&m);
scanf(“%f”,&n);
if(opt==1){
r=m+n;
}else if(opt==2){
r=m-n;
}else if(opt==3){
r=m*n;
}else{
r=m/n;
}
printf(“\n%f\n”,r);
}
system(“pause”);
return 0;
}

ESCRIBA UN PROGRAMA QUE CALCULE UN VALOR APROXIMADO DE LA SIGUIENTE SERIE INFINITA EN UN PUNTO x: ex=1+x+x2/2!+x3/3!+…+xn/n!+…

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

int main(int argc, char *argv[])
{
double x,ex=1,tn=1,i=1;
printf(“Introduzca el exponente: “);
scanf(“%lf”,&x);
while(tn>0.000001){
tn*=x/i;
ex+=tn;
i++;
}
printf(“e^x=%lf\n”,ex);
system(“pause”);
return 0;
}

Códigos escritos por Santiago Codesido Sánchez

MODIFIQUE EL PROGRAMA NÚMERO 4 DE LA SESIÓN ANTERIOR PARA QUE CALCULE LA RAÍZ CUADRADA DE UN NÚMERO, QUE PUEDE SER POSITIVO O NEGATIVO.

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

int main(int argc, char *argv[])
{
int n;
printf(“Introduzca el número: “);
scanf(“%d”,&n);
if(n<0){
printf(“La raíz cuadrada de %d es %lfi\n”,n,sqrt(-n));
}else{
printf(“La raíz cuadrada de %d es %lf\n”,n,sqrt(n));
}
system(“pause”);
return 0;
}
ESCRIBA UN PROGRAMA QUE RECIBA UN NÚMERO Y LO CLASIFIQUE COMO NEGATIVO, POSITIVO MENOR O IGUAL QUE 100 O POSITIVO MAYOR QUE 100 .

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n;
printf(“Introduzca un número:”);
scanf(“%d”,&n);
printf(“\n\n%d es “,n);
if(n<0){
printf(“negativo.\n”);
}else if(n<=100){
printf(“mayor que 0 y menor o igual que 100.\n”);
}else{
printf(“mayor que 100.\n”);
}
system(“pause”);
return 0;
}

ESCRIBA UN PROGRAMA QUE RECIBA UNA NOTA Y LA CLASIFIQUE DE ACUERDO AL SIGUIENTE CRITERIO: SUSPENSO (SI LA NOTA ES MAYOR QUE 0 Y MENOR QUE 5.0), APROBADO (SI ES MAYOR O IGUAL QUE 5.0 Y MENOR QUE 7.0), NOTABLE (SI ES MAYOR O IGUAL QUE 7.0 Y MENOR QUE 9.0), SOBRESALIENTE (SI ES MAYOR O IGUAL QUE 9.0 Y MENOR QUE 10.0), MATRÍCULA DE HONOR (SI ES 10.0) Y ERRÓNEA EN CUALQUIER OTRO CASO.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
float n;
printf(“Introduzca la nota:”);
scanf(“%f”,&n);
if(n<0 || n>10){
printf(“Error de formato\n”);
system(“pause”);
return 1;
}
printf(“\nEl alumno tiene “);
if(n<5){
printf(“suspenso.\n”);
}else if(n<7){
printf(“aprobado.\n”);
}else if(n<9){
printf(“notable.\n”);
}else if(n<10){
printf(“sobresaliente.\n”);
}else{
printf(“matrícula de honor.\n”);
}
system(“pause”);
return 0;
}

ESCRIBA UN PROGRAMA QUE CALCULE EL FACTORIAL DE UN NÚMERO.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n,f=1;
printf(“Introduzca un número: “);
scanf(“%d”,&n);
if(n<0){
printf(“n>0\n”);
system(“pause”);
return 1;
}
for(int i=1;i<=n;i++){
f*=i;
}
printf(“n!=%d\n”,f);
system(“pause”);
return 0;
}

ESCRIBA UN PROGRAMA QUE CALCULE LA SUMA DE LOS N PRIMEROS TÉRMINOS DE LA SIGUIENTE SERIE: 1/(r*r), PARA r=1,2,…,N.

#include <stdio.h>
#include <stdlib.h>
//Suma de los n primeros miembros de la serie Sumatorio de 1/(i*i)
int main(int argc, char *argv[])
{
int n;
double S=0,x;
printf(“n=”);
scanf(“%d”,&n);
if(n<=0){
printf(“n>0\n”);
system(“pause”);
return 1;
}
for(int i=1;i<=n;i++){
x=i;
S+=1/(x*x);
}
printf(“S=%lf\n”,S);
system(“pause”);
return 0;
}

ESCRIBA UN PROGRAMA QUE, A PARTIR DEL NÚMERO DE FILAS (f) Y COLUMNAS (c) PROPORCIONADAS POR EL USUARIO, IMPRIMA EN PANTALLA UNA TABLA DE ASTERISCOS DE TAMAÑO fxc.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int f,c;
printf(“Número de filas:”);
scanf(“%d”,&f);
printf(“Número de columnas:”);
scanf(“%d”,&c);
if(f<=0||c<=0){
printf(“Error\n”);
system(“pause”);
return 1;
}
for(int i=0;i<f;i++){
for(int j=0;j<c;j++){
printf(“*”);
}
printf(“\n”);
}
system(“pause”);
return 0;
}

Códigos escritos por Santiago Codesido Sánchez

ESCRIBA UN PROGRAMA QUE CALCULE LA SUPERFICIE DE UNA ESFERA (S=4*3.1416*r*r) A PARTIR DEL RADIO (r)

#include <stdio.h>
#define PI 3.14159

int main(int argc, char *argv[])
{
int rad;
printf(“Introduzca el valor del radio (como entero): “);
scanf(“%d”,&rad);
printf(“La superficie de la esfera es %f\n”,rad*PI*4);

system(“PAUSE”)
return 0;
}

ESCRIBA UN PROGRAMA QUE CALCULE EL ÁREA DE UN CÍRCULO, LA LONGITUD DE LA CIRCUNFERENCIA QUE LO DELIMITA, SU VOLUMEN (v=4*3.1416*r*r*r/3) Y SU SUPERFICIE (S=4*3.1416*r*r) A PARTIR DEL RADIO (r).

#include <stdio.h>
#include <stdlib.h>
#define PI 3.14159

int main(int argc, char *argv[])
{
float rad;
printf(“Introduzca el valor del radio (como entero): “);
scanf(“%f”,&rad);
printf(“La longitud de la circunferencia es %f\nEl área del círculo es %f\nEl volumen de la esfera es %f\nLa superficie de la esfera es %f\n”,2*PI*rad,rad*rad*PI,rad*rad*rad*4*PI/3,rad*rad*PI*4);
system(“pause”);
return 0;
}

ESCRIBA UN PROGRAMA QUE CALCULE LA SUMA, DIFERENCIA Y PRODUCTO DE DOS NÚMEROS.

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

int main(int argc, char *argv[])
{
float m,n;
printf(“Introduzca los valores de los números m y n:\n”);
scanf(“%f”,&m);
scanf(“%f”,&n);
printf(“m+n=%f\nm-n=%f\nm*n=%f\n”,m+n,m-n,m*n);
system(“pause”);
return 0;
}

ESCRIBA UN PROGRAMA QUE CALCULE LA RAÍZ CUADRADA DE UN NÚMERO POSITIVO.

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

int main(int argc, char *argv[])
{
int n;
printf(“Introduzca el número: “);
scanf(“%d”,&n);
printf(“La raíz cuadrada de %d es %lf\n”,n,sqrt(n));
system(“pause”);
return 0;
}