main() { int *vet; //Declaro um vetor de inteiros int i=1; int n, x1, x2, dif1, dif2; //Declaroção algumas variáveis que serão utilizados ao longo do programa printf("Digite o numero de inteiros a serem lidos (maior ou igual a 2): "); //Entra com o número n>=2 que define o total de valores a serem lidos scanf("%d", &n); while(n<=2) //Caso o usuário digite um valor menor do que 2 { printf("Eh necessário, pelo menos, um par de inteiros para o programa funcionar\nDigite um valor maior ou igual a dois:"); scanf("%d", &n); } vet=(int*)malloc(sizeof(int)*n); //Alocação de memória para um vetor com n elementos read_numbers(vet, n); //Leitura das entradas e armazenamento no vetor insertion(vet, n); //Ordenação do vetor por meio do algoritmo de ordenação insertion dif1=vet[1]-vet[0]; x1=vet[0]; x2=vet[1]; while(i<n-2) //Percorre o vetor buscando o par de números com a menor diferença e armazena-os nas variáveis x1 e x2 { dif2=vet[i+1]-vet[i]; if(dif2<dif1) { x1=vet[i]; x2=vet[i+1]; dif1=dif2; } i++; } printf("O par de numero com menor diferenca eh:\n\n"); //Imprime o par de número com a menor diferença printf("%d e %d", x1, x2); free(vet); //Desalocaçãode memória utilizada getch(); }
int main(int argc, char **argv) { if (argc != 2) { printf("usage: ./array0 <valid file path>\n"); return 1; } if (argv[1] == NULL) { printf("can not process empty file name\n"); return 2; } FILE* file = fopen(argv[1], "r"); if (file == NULL) { printf("can not open file %s\n", argv[1]); return 2; } int_array numbers = read_numbers(file); print_array(numbers); remove_array(&numbers); fclose(file); return 0; }
int main() { init_numbers(N, nums); read_numbers(nums); print_numbers(N, nums); find_max(N, nums); return 0; }
int main (void) { int num = 5; struct node *my_node = malloc(sizeof(struct node)); my_node->value = num; my_node->next = NULL; read_numbers(); printf("my_node add: %p\n", (void *)my_node); print_list(top); top = insert_into_ordered_list(top, my_node); print_list(top); print_list(find_last(top, 2)); delete_from_list(&top, num); print_list(top); return 0; }
int main (int argc, char *argv[]) { long px, py; size_t intc; long *intv; if (argc != 4) return usage (); // the first to arguments are interpreted respectively as X and Y coordinates of the point of interest px = require_number (argv[1]); py = require_number (argv[2]); // the third argument is interpreted as the filename // from which we are reading intv = read_numbers (argv[3], &intc); if (intv == NULL) { (void)puts ("main: error parsing points file"); return EXIT_FAILURE; } if (intc % 2 != 0) { printf ("main: got %lu numbers, but expected an even number (pairs)\n", (unsigned long)intc); xfree (intv); return EXIT_FAILURE; } // call the algorithm that calculates wheter the point is inside the polyon { rc_ctx *ctx = rc_new (intc, intv); printf ("%s\n", rc_contains (ctx, px, py) ? "inside" : "outside"); rc_delete (ctx); } xfree (intv); return EXIT_SUCCESS; }
int main() { try { // read N - amount of numbers to calculate sum of int amount = read_amount(); // read set of numbers vector<double> numbers = read_numbers(); // output sum output_sum(calc_sum(numbers, amount), amount); // output diff numbers output_diff_vector(calc_diff_vector(numbers)); } catch (exception& e) { cerr << e.what(); return 1; } catch (...) { cerr << "Unknown error"; return 2; } }
int main(int argc, char **argv) { if (argc != 2) { printf("usage: ./list0 <valid file path>\n"); return 1; } if (argv[1] == NULL) { printf("can not process empty file name\n"); return 2; } FILE* file = fopen(argv[1], "r"); if (file == NULL) { printf("can not open file %s\n", argv[1]); return 3; } int_list* list = read_numbers(file, NULL); fclose(file); print_list(list); printf("what number are you looking for?\n"); int number = get_int(); if (index_of(list, number) == -1) { printf("404 Not Found!\n"); } else { printf("List contains %d\n", number); } remove_list(list); return 0; }
/* Fill an array of int values, prompting the user from stdin; * echo the list of numbers entered, then find the max and print it */ int main() { // Prompt the user for the size of the array // Store the size in the static integer SIZE get_size(); if(SIZE<=0){ fprintf(stderr, "Error! The size must be greater than 0!\n"); exit(-1); } // Create a new array of numbers, of size "SIZE". int numbersArray[SIZE]; // Pass the pointer to the array into these functions // Initialize all elements in the array to -1 init_numbers(SIZE, numbersArray); // Read the user input read_numbers(numbersArray); // Print all values in the array print_numbers(SIZE, numbersArray); // Print the max values, evaluated by our functions printf("Max number: %d\n", find_max(SIZE, numbersArray)); printf("Max number out of first half: %d\n", find_max(SIZE/2, numbersArray)); }
SLterminfo_Type *_pSLtt_tigetent (SLCONST char *term) { SLCONST char **tidirs, *tidir; FILE *fp = NULL; char file[1024]; static char home_ti [1024]; char *env; SLterminfo_Type *ti; if ( (term == NULL) #ifdef SLANG_UNTIC && (SLang_Untic_Terminfo_File == NULL) #endif ) return NULL; if (_pSLsecure_issetugid () && ((term[0] == '.') || (NULL != strchr (term, '/')))) return NULL; if (NULL == (ti = (SLterminfo_Type *) SLmalloc (sizeof (SLterminfo_Type)))) { return NULL; } memset ((char *)ti, 0, sizeof (SLterminfo_Type)); #ifdef SLANG_UNTIC if (SLang_Untic_Terminfo_File != NULL) { fp = open_terminfo (SLang_Untic_Terminfo_File, ti); goto fp_open_label; } else #endif /* If we are on a termcap based system, use termcap */ if (0 == tcap_getent (term, ti)) return ti; if (NULL != (env = _pSLsecure_getenv ("TERMINFO"))) Terminfo_Dirs[0] = env; if (NULL != (env = _pSLsecure_getenv ("HOME"))) { strncpy (home_ti, env, sizeof (home_ti) - 11); home_ti [sizeof(home_ti) - 11] = 0; strcat (home_ti, "/.terminfo"); Terminfo_Dirs [1] = home_ti; } tidirs = Terminfo_Dirs; while (NULL != (tidir = *tidirs++)) { if (*tidir == 0) continue; if (sizeof (file) > strlen (tidir) + 5 + strlen (term)) { sprintf (file, "%s/%c/%s", tidir, *term, term); if (NULL != (fp = open_terminfo (file, ti))) break; sprintf (file, "%s/%02x/%s", tidir, (unsigned char)*term, term); if (NULL != (fp = open_terminfo (file, ti))) break; } } #ifdef SLANG_UNTIC fp_open_label: #endif if (fp == NULL) { SLfree ((char *) ti); return NULL; } ti->flags = SLTERMINFO; if ((NULL == read_terminal_names (fp, ti)) || (NULL == read_boolean_flags (fp, ti)) || (NULL == read_numbers (fp, ti)) || (NULL == read_string_offsets (fp, ti)) || (NULL == read_string_table (fp, ti))) { _pSLtt_tifreeent (ti); ti = NULL; } (void) fclose (fp); return ti; }
SLterminfo_Type *_SLtt_tigetent (char *term) { char *tidir; int i; FILE *fp = NULL; char file[1024]; static char home_ti [1024]; char *home; SLterminfo_Type *ti; if ( (term == NULL) #ifdef SLANG_UNTIC && (SLang_Untic_Terminfo_File == NULL) #endif ) return NULL; if (NULL == (ti = (SLterminfo_Type *) SLmalloc (sizeof (SLterminfo_Type)))) { return NULL; } #ifdef SLANG_UNTIC if (SLang_Untic_Terminfo_File != NULL) { fp = open_terminfo (SLang_Untic_Terminfo_File, ti); goto fp_open_label; } else #endif /* If we are on a termcap based system, use termcap */ if (0 == tcap_getent (term, ti)) return ti; if (NULL != (home = getenv ("HOME"))) { strncpy (home_ti, home, sizeof (home_ti) - 11); home_ti [sizeof(home_ti) - 11] = 0; strcat (home_ti, "/.terminfo"); Terminfo_Dirs [0] = home_ti; } Terminfo_Dirs[1] = getenv ("TERMINFO"); i = 0; while (1) { tidir = Terminfo_Dirs[i]; if (tidir != NULL) { if (*tidir == 0) break; /* last one */ if (sizeof (file) >= strlen (tidir) + 4 + strlen (term)) { sprintf (file, "%s/%c/%s", tidir, *term, term); if (NULL != (fp = open_terminfo (file, ti))) break; } } i++; } #ifdef SLANG_UNTIC fp_open_label: #endif if (fp != NULL) { if (NULL != read_terminal_names (fp, ti)) { if (NULL != read_boolean_flags (fp, ti)) { if (NULL != read_numbers (fp, ti)) { if (NULL != read_string_offsets (fp, ti)) { if (NULL != read_string_table (fp, ti)) { /* success */ fclose (fp); ti->flags = SLTERMINFO; return ti; } SLfree ((char *)ti->string_offsets); } SLfree ((char *)ti->numbers); } SLfree ((char *)ti->boolean_flags); } SLfree ((char *)ti->terminal_names); } fclose (fp); } SLfree ((char *)ti); return NULL; }
// Reading the MCNP file MCNPError McnpData::read_mcnpfile(bool skip_mesh) { MCNPError result; moab::ErrorCode MBresult; moab::CartVect tvect; std::vector<double> xvec[3]; // Open the file std::ifstream mcnpfile; mcnpfile.open( MCNP_filename.c_str() ); if (!mcnpfile) { std::cout << "Unable to open MCNP data file." << std::endl; return MCNP_FAILURE; } std::cout << std::endl; std::cout << "Reading MCNP input file..." << std::endl; // Prepare for file reading ... char line[10000]; int mode = 0; // Set the file reading mode to read proper data int nv[3]; // Read in the file ... while (! mcnpfile.eof() ) { mcnpfile.getline(line, 10000); // std::cout << line << std::endl; switch(mode) { case 0: // First line is a title mode++; break; case 1: // Coordinate system mode++; result = read_coord_system(line); if (result == MCNP_FAILURE) return MCNP_FAILURE; break; case 2: // Rotation matrix mode++; for (int i = 0; i < 4; i++) { mcnpfile.getline(line, 10000); result = read_rotation_matrix(line, i); if (result == MCNP_FAILURE) return MCNP_FAILURE; } if (skip_mesh) return MCNP_SUCCESS; break; case 3: // Read in vertices and build elements mode++; for (int i = 0; i < 3; i++) { // How many points in the x[i]-direction nv[i] = how_many_numbers(line); if (nv[i] <= 0) return MCNP_FAILURE; // Get space and read in these points result = read_numbers(line , nv[i], xvec[i]); if (result == MCNP_FAILURE) return MCNP_FAILURE; // Update to the next line mcnpfile.getline(line, 10000); } // Make the elements and vertices result = make_elements(xvec, nv); if (result == MCNP_FAILURE) return MCNP_FAILURE; break; case 4: // Read in tally data, make, and tag elements mode++; moab::EntityHandle elemhandle; moab::EntityHandle vstart, vijk; moab::EntityHandle connect[8]; // double d[3]; // vstart = MCNP_vertices.front(); vstart = *(vert_handles.begin()); for (int i=0; i < nv[0]-1; i++) { for (int j=0; j < nv[1]-1; j++) { for (int k=0; k < nv[2]-1; k++) { vijk = vstart + (i + j*nv[0] + k*nv[0]*nv[1]); //std::cout << vijk << std::endl; connect[0] = vijk; connect[1] = vijk + 1; connect[2] = vijk + 1 + nv[0]; connect[3] = vijk + nv[0]; connect[4] = vijk + nv[0]*nv[1]; connect[5] = vijk + 1 + nv[0]*nv[1]; connect[6] = vijk + 1 + nv[0] + nv[0]*nv[1]; connect[7] = vijk + nv[0] + nv[0]*nv[1]; MBresult = MBI->create_element(moab::MBHEX, connect, 8, elemhandle); if (MBresult != moab::MB_SUCCESS) return MCNP_FAILURE; elem_handles.insert(elemhandle); mcnpfile.getline(line, 10000); result = extract_tally_data(line, elemhandle); if (result == MCNP_FAILURE) return MCNP_FAILURE; } } } /* for (MBRange::iterator rit=vert_handles.begin(); rit != vert_handles.end(); ++rit) { std::cout << *rit << std::endl; } for (int i=0; i < nv[0]-1; i++) { for (int j=0; j < nv[1]-1; j++) { for (int k=0; k < nv[2]-1; k++) { vijk = vstart + (i + j*nv[0] + k*nv[0]*nv[1]); connect[0] = vijk; connect[1] = vijk + 1; connect[2] = vijk + 1 + nv[0]; connect[3] = vijk + nv[0]; connect[4] = vijk + nv[0]*nv[1]; connect[5] = vijk + 1 + nv[0]*nv[1]; connect[6] = vijk + 1 + nv[0] + nv[0]*nv[1]; connect[7] = vijk + nv[0] + nv[0]*nv[1]; MBresult = MBI->create_element(MBHEX, connect, 8, elemhandle); if (MBresult != MB_SUCCESS) return MCNP_FAILURE; elem_handles.insert(elemhandle); mcnpfile.getline(line, 10000); result = extract_tally_data(line, elemhandle); if (result == MCNP_FAILURE) return MCNP_FAILURE; } } } */ break; case 5: // Ckeck for weirdness at end of file if (! mcnpfile.eof() ) return MCNP_FAILURE; break; } } std::cout << "SUCCESS! Read in " << elem_handles.size() << " elements!" << std::endl << std::endl; // MCNP_vertices.clear(); vert_handles.clear(); MCNP_elems.clear(); return MCNP_SUCCESS; }