Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
PRIMER PROGRAMA CON MALLOC()
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
/* ANÁLISIS * Entradas --> * Desnivel máximo permitido D --> unsigned int * Cantidad de cotas de altura N --> unsigned long long * N cotas de altura vectorCotas --> unsigned int * * Salida --> * Mensaje: "APTA" o "NO APTA" * * Diseño * * repetir * Leer D * Leer N * dimensionar el vector * ver si hay espacio en el heap, sino finalizar el programa * poblar el vector con las cotas de altura * Analizar si es apta o no la excursión. * hasta que N == 0 */ #include <stdio.h> #include <stdlib.h> #include <stdbool.h> // ------------------------Prototipos----------------------------- typedef unsigned long long ULL; void leerCotas( unsigned int *, ULL); bool esApta(unsigned int *, ULL, unsigned int); // ----------------------Programa Principal---------------------------- int main(void) { unsigned int desnivel; ULL cantidadCotas; unsigned int *vectorCotas = NULL; // ----------------------Proceso---------------------------- do { // Entradas scanf("%u", &desnivel); scanf("%llu", &cantidadCotas); if ( cantidadCotas == 0) break; vectorCotas = (unsigned int *) malloc ( sizeof(unsigned int) * cantidadCotas ); if ( vectorCotas == NULL) exit(EXIT_FAILURE); leerCotas( vectorCotas, cantidadCotas ); if ( esApta( vectorCotas, cantidadCotas, desnivel ) ) printf("APTA\n"); else printf("NO APTA\n"); free(vectorCotas); }while( 1 ); printf("--- FIN ---"); return 0; } void leerCotas( unsigned int *array, ULL quantity) { unsigned int i; for (i = 0 ; i < quantity ; i ++ ) scanf("%u", &array[i]); } bool esApta(unsigned int *array, ULL quantity, unsigned int unevenness) { unsigned int i; unsigned int sum = 0; for ( i = 0 ; i < quantity - 1 ; i ++) { if ( array[i] >= array[i + 1] ) sum = 0; else { sum = sum + (array[i + 1] - array[i]); if ( sum > unevenness ) return false; } } return true; }
gcc
39 5 10 20 30 40 50 10 5 50 55 32 20 10 10 5 20 20 31 20 20 10 5 10 20 10 21 10 10 5 10 20 31 15 15 10 5 10 10 19 15 20 16 5 20 10 25 25 25 10 5 20 10 10 10 22 0 0
Show compiler warnings
[
+
] Compiler args
[
-
]
Show input
Compilation time: 0.25 sec, absolute running time: 0.12 sec, cpu time: 0.05 sec, memory peak: 3 Mb, absolute service time: 0,38 sec
edit mode
|
history
|
discussion
NO APTA APTA NO APTA NO APTA NO APTA APTA APTA NO APTA --- FIN ---