I have 4 functions that run through a text file and store the values to their designated arrays decalred at the beginning of the problem. The issue i'm having is that when I update one of the arrays the others all are never updated or are reset. So for example if I run the getkosDS function it will update the array and print out the value i am looking for. The other arrays will contain zero. However if I comment out the getkosDS function the next array (nipsDS) will update and not be zero!!! I am very confused.
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int kosND;
int kosNW;
int kosNZC;
int enronND;
int enronNW;
int enronNZC;
int nipsND;
int nipsNW;
int nipsNZC;
int nytND;
int nytNW;
int nytNZC;
int world_size;
int my_rank;
int kosDS[3430];
int nipsDS[1500];
int enronDS[39861];
int nytDS[300000];
void getKOS(){
int i;
FILE *MyFile;
char line[25];
MyFile=fopen("/home/mcconnel/BagOfWords/docword.kos.txt","r");
for(i=0; i<5; i++){
fscanf(MyFile, "%s", line);
if(i == 0){
kosND = atoi(line);
}
if(i == 1){
kosNW = atoi(line);
}
if(i == 2){
kosNZC = atoi(line);
}
}
fclose(MyFile);
}
void getEnron(){
int i;
FILE *MyFile;
char line[25];
MyFile=fopen("/home/mcconnel/BagOfWords/docword.enron.txt","r");
for(i=0; i<5; i++){
fscanf(MyFile, "%s", line);
if(i == 0){
enronND = atoi(line);
}
if(i == 1){
enronNW = atoi(line);
}
if(i == 2){
enronNZC = atoi(line);
}
}
}
void getNips(){
int i;
FILE *MyFile;
char line[25];
MyFile=fopen("/home/mcconnel/BagOfWords/docword.nips.txt","r");
for(i=0; i<5; i++){
fscanf(MyFile, "%s", line);
if(i == 0){
nipsND = atoi(line);
}
if(i == 1){
nipsNW = atoi(line);
}
if(i == 2){
nipsNZC = atoi(line);
}
}
fclose(MyFile);
}
void getNYT(){
int i;
FILE *MyFile;
char line[25];
MyFile=fopen("/home/mcconnel/BagOfWords/docword.nytimes.txt","r");
for(i=0; i<5; i++){
fscanf(MyFile, "%s", line);
if(i == 0){
nytND = atoi(line);
}
if(i == 1){
nytNW = atoi(line);
}
if(i == 2){
nytNZC = atoi(line);
}
}
fclose(MyFile);
}
void getKosDS(){
int i;
int z;
FILE *MyFile;
char line[25];
MyFile=fopen("/home/mcconnel/BagOfWords/docstart.kos.txt","r");
for(i = 0; i<3430; i++){
fscanf(MyFile, "%s", line);
if(i != 0 && i % 2 == 1){
kosDS[z] = atoi(line);
z++;
}
}
fclose(MyFile);
}
int getNipsDS(){
int i;
int z;
FILE *MyFile;
char line[25];
MyFile=fopen("/home/mcconnel/BagOfWords/docstart.nips.txt","r");
for(i = 0; i<1500; i++){
fscanf(MyFile, "%s", line);
if(i != 0 && i % 2 == 1){
nipsDS[z] = atoi(line);
z++;
}
}
fclose(MyFile);
}
int main(int argc, char** argv)
{ MPI_Init(NULL,NULL);
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// printf("\n%d", world_size);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
// printf("\n%d", my_rank);
if(my_rank == 0){
getKOS();
getEnron();
getNips();
getNYT();
getKosDS();
// getEnronDS();
// getNytDS();
printf("\ngetting complete\n");
printf("KOS location %d\n", kosDS[5]);
// printf("Enron location %d\n", enronDS[200]);
printf("Nips location %d\n", nipsDS[500]);
// printf("NYT location %d\n", nytDS[10000]);
}
else{
printf("\n%d \n", my_rank);
}
MPI_Finalize();
}
this is the result
KOS location 4875
Nips location 0
however if I were to comment out getKosDS the result is
KOS location 0
Nips location 28340
Craziness, if you can't tell I'm new to C
In getKosDS the z is uninitialized, so by writing to nipsDS[z] you are actually writing into a random memory location, invoking an undefined behavior.
Update:
The same problem is in the getNipsDS function.
Related
I am trying to create a program to solve a wordsearch from a 2D array using only pointers. In my primary function for doing the actual search for the words I have a while loop that is supposed to keep going as long as i doesn't exceed the length of the word and as long as the letters from the word correspond to the letters on the grid. After finding the word it should make the letters on the grid lowercase and print out that it found the word. Here is my entire program right now.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void printPuzzle(char** arr, int n);
void searchPuzzle(char** arr, int n, char** list, int listSize);
void searchWord(int j, int k, int gridsize, char** arr, char** list, int listPos);
int main(int argc, char **argv) {
int bSize = 15;
if (argc != 2) {
fprintf(stderr, "Usage: %s <puzzle file name>\n", argv[0]);
return 2;
}
int i, j;
FILE *fptr;
char **block = (char**)malloc(bSize * sizeof(char*));
char **words = (char**)malloc(50 * sizeof(char*));
fptr = fopen(argv[1], "r");
if (fptr == NULL) {
printf("Cannot Open Puzzle File!\n");
return 0;
}
for(i=0; i<bSize; i++){
*(block+i) = (char*)malloc(bSize * sizeof(char));
fscanf(fptr, "%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n", *(block+i), *(block+i)+1, *(block+i)+2, *(block+i)+3, *(block+i)+4, *(block+i)+5, *(block+i)+6, *(block+i)+7, *(block+i)+8, *(block+i)+9, *(block+i)+10, *(block+i)+11, *(block+i)+12, *(block+i)+13, *(block+i)+14 );
}
fclose(fptr);
fptr = fopen("states.txt", "r");
if (fptr == NULL) {
printf("Cannot Open Words File!\n");
return 0;
}
for(i=0; i<50; i++){
*(words+i) = (char*)malloc(20 * sizeof(char));
fgets(*(words+i), 20, fptr);
}
for(i=0; i<49; i++){
*(*(words+i) + strlen(*(words+i))-2) = '\0';
}
printf("Printing list of words:\n");
for(i=0; i<50; i++){
printf("%s\n", *(words + i));
}
printf("\n");
printf("Printing puzzle before search:\n");
printPuzzle(block, bSize);
printf("\n");
searchPuzzle(block, bSize, words, 50);
printf("\n");
printf("Printing puzzle after search:\n");
printPuzzle(block, bSize);
printf("\n");
return 0;
}
void printPuzzle(char** arr, int n){
for(int i = 0; i < n; i++){
printf("\n");
for(int j = 0; j < 15; j++){
printf("%c ", *(*(arr+i)+j));
}
}
}
void searchPuzzle(char** arr, int n, char** list, int listSize){
for(int i = 0; i < listSize; i++){
for(int j = 0; j < 15; j++){
for(int k = 0; k < 15; k++){
searchWord(j, k, 15, arr, list, i);
}
}
}
}
void searchWord(int j, int k, int gridsize, char** arr, char** list, int listPos){
int wordlength = strlen(*(list+listPos));
if(j+wordlength <= gridsize){ //Horizontal
int i = 0;
while(i < wordlength && *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
i++;
}
if(i == wordlength){
while(i > 0 && *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
*(*(arr+(j+i))+k) = tolower(*(*(arr+(j+i))+k));
i--;
}
printf("Word found: ");
for(i = 0; i < wordlength; i++)
printf("%c", *(*(list+listPos)+i));
}
}
if(k+wordlength <= gridsize){ //Vertical
int i = 0;
while(i < wordlength && *(*(arr+j)+(k+i)) == *(*(list+listPos)+i)){
i++;
}
if(i == wordlength){
while(i > 0 && *(*(arr+j)+(k+i)) == *(*(list+listPos)+i)){
*(*(arr+(j+i))+k) = tolower(*(*(arr+(j+i))+k));
i--;
}
printf("Word found: ");
for(i = 0; i < wordlength; i++){
printf("%c", *(*(list+listPos)+i));
}
}
}
if(j+wordlength <= gridsize && k+wordlength <= gridsize){ //Diagonal
int i = 0;
while(i < wordlength && *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
i++;
}
if(i == wordlength){
while(i > 0 && *(*(arr+(j+i))+(k+i)) == *(*(list+listPos)+i)){
*(*(arr+(j+i))+(k+i)) = tolower(*(*(arr+(j+i))+(k+i)));
i--;
}
printf("Word found: ");
for(i = 0; i < wordlength; i++)
printf("%c", *(*(list+listPos)+i));
}
}
}
This is the part I believe there is a problem with.
while(i < wordlength && *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
i++;
}
If I print out the decimal value of i after this while loop it should at some point approach the length of one of the words as I know some words are horizontal, however every time it loops through and prints the value of i it never exceeds 1 which means the while loop never runs more than once which is impossible with the wordsearch files I am using to test this. What is causing this to happen? It is also 0 or 1 for every other direction when I try, the example above was just the while loop from the horizontal search.
There may also be a problem with the function searchPuzzle and the amount it loops through. However, I think that may be just my imagination.
Additional Info: The grid is 15x15 and there are 50 words in the list.
I think that you might need to declare Wordsearch again after the && -
while(i < wordlength && wordlength *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
i++;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vettore.h"
int main(int argc, char *argv[]){
FILE *result = fopen("result.txt", "w");
int cont = 0, i, n;
char *s1;
if(argc != 1)
printf("Numero parametri non corretto\n");
else{
FILE *fp = fopen("test_suite.txt", "r");
while(fscanf(fp, "%s %d", s1, &n) == 2)
cont++;
rewind(fp);
for(i=0; i<cont; i++){
fscanf(fp, "%s %d", s1, &n);
int *a = (int*) calloc(n, sizeof(int));
char *s2;
strcpy(s2, s1);
finput_array(strcat(s1,"_input.txt"), a, n);
strcpy(s1,s2);
bubblesort(a, n);
foutput_array(strcat(s1, "_output.txt"), a, n);
strcpy(s1,s2);
int *oracle = (int*) calloc(n, sizeof(int));
finput_array(strcat(s1, "_oracle.txt"), oracle, n);
strcpy(s1,s2);
if(confronta_array(a, oracle, n))
fprintf(result, "%s PASS\n", s1);
else
fprintf(result, "%s FAIL\n", s1);
free(a);
free(oracle);
}
fclose(fp);
}
fclose(result);
}
This are "vettore.c" functions:
void bubblesort(int a[], int n){
int i, j;
for(i = 0 ; i < n - 1; i++)
{
for(j = 0 ; j < n - i - 1; j++)
{
if (a[j] > a[j+1]) /* For decreasing order use < */
{
scambia(&a[j], &a[j+1]);
}
}
}
}
void finput_array(char *file_name, int a[], int n){
FILE *fd = fopen(file_name, "r");
if(fd == NULL)
printf("Errore in apertura del file %s\n", file_name);
else{
for(int i=0; i<n; i++)
fscanf(fd, "%d", &a[i]);
fclose(fd);
}
}
void foutput_array(char *file_name, int a[], int n){
int i;
FILE *fd;
fd = fopen(file_name, "w");
if(fd == NULL)
printf("Errore in apertura del file %s\n", file_name);
else{
for(i=0; i<n; i++)
fprintf(fd, "%d\n", a[i]);
fclose(fd);
}
}
int confronta_array(int a[], int b[], int n){
int i=0;
while(i<n && a[i] == b[i])
i++;
return (i==n) ? 1 : 0;
}
The program doesn't write anything in "result.txt". Why?
It should write those sentences.
In this part:
while(fscanf(fp, "%s %d", s1, &n) == 2) cont++;
Maybe "cont" dont increment because "test_suite.txt" don't have the "fscanf" format requirements.
#include <stdio.h>
#include <stdlib.h>
#include "vettore.h"
int main(int argc, char *argv[]){
if(argc != 5)
printf("Incorrect parameters number\n");
else{
int n = atoi(argv[1]);
int *a = (int*) calloc(n, sizeof(int));
if(a == NULL)
printf("Unsufficient memory\n");
else{
finput_array(argv[2], a, n);
bubblesort(a, n);
foutput_array(argv[4], a, n);
int *oracle = (int*) calloc(n, sizeof(int));
finput_array(argv[3], oracle, n);
if(compare_array(a, oracle, n))
printf("PASS\n");
else
printf("FAIL\n");
}
}
}
I run the program this way: ./test_ordina_array.exe 12 TC4_input.txt TC4_oracle.txt TC4_output.txt but it gives me segmentation fault.
"TC4_output.txt" is created by the program while the other two files already exist.
This are the functions used:
void bubblesort(int a[], int n){
int i, j;
for(i = 0 ; i < n - 1; i++)
{
for(j = 0 ; j < n - i - 1; j++)
{
if (a[j] > a[j+1]) /* For decreasing order use < */
{
swap(&a[j], &a[j+1]);
}
}
}
}
void finput_array(char *file_name, int a[], int *n){
FILE *fd = fopen(file_name, "r");
if(fd == NULL)
printf("Errore in apertura del file %s\n", file_name);
else{
int i = 0;
fscanf(fd, "%d", &a[i]);
while(i<*n && !feof(fd)){
i++;
fscanf(fd, "%d", &a[i]);
}
fclose(fd);
if(i<*n)
*n = i;
}
}
void foutput_array(char *file_name, int a[], int n){
int i;
FILE *fd;
fd = fopen(file_name, "w");
if(fd == NULL)
printf("Errore in apertura del file %s\n", file_name);
else{
for(i=0; i<n; i++)
fprintf(fd, "%d\n", a[i]);
fclose(fd);
}
}
int compare_array(int a[], int b[], int n){
int i=0;
while(i<n && a[i] == b[i])
i++;
return (i==n) ? 1 : 0;
}
They are contained in "vettore.c" and "vettore.h" contains their prototypes.
The program has to order in ascending order the elements contained in the first txt file and write them in the output file.
You have problem when using finput_array
finput_array(argv[2], a, n);
Please replace by
finput_array(argv[2], a, &n);
I have an Instance File from which I need to store the NUM_PT and all the respective co-ordinates in the form of a 2D array system (personal choice so I can access them easily). I am able to retrieve the NUM_PT but I am stuck at reading the successive cordinates into my array.
HERE IS WHAT I HAVE DONE
/* Assignment 2 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#define MAXS 256
int main(int argc, char *argv[])
{
int num_pt;
int inputfile = 0, outputfile = 0, i;
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "-i") == 0)
inputfile = i+1;
if (strcmp (argv[i], "-o") == 0)
outputfile = i+1;
}
if (inputfile == 0)
{
/* invalid command line options */
printf("\nIncorrect command-line...\n");
printf("> %s [-i inputfile [-o outputfile]]\n\n", argv[0]);
exit(0);
}
FILE *fp;
fp = fopen(argv[inputfile], "r");
int count = 0;
if (fp == 0)
{
printf("\nCould not find %s\n", argv[inputfile]);
exit(0);
}
char line[MAXS];
while (fgets(line, sizeof line, fp) != NULL)
{
if (count == 4)
{
fscanf(fp, "%d", &num_pt);
break;
}
else
count++;
}
int arr[num_pt][1];
while (fgets(line, sizeof line, fp) != NULL)
{
if (count == 5)
{
int k, j, cord;
for (k = 0; k < num_pt; k++)
{
for (j = 0; j < num_pt; j++)
{
while (fscanf(fp, "%d%d", &cord) > 0)
{
arr[k][j] = cord;
j++;
}
}
}
}
}
fclose(fp)
return 0;
}
After retrieving NUM_PT i tried reinitializing the count to 5 because the cordinates start from **LINE 6* in the file.
ERROR FROM COMPILER
Language: c99 ; Compiler: gcc
sample for "Storing numbers as (x, y) cordinates from a file" (It is better not to fix the reading position)
#include <stdio.h>
typedef struct point {
int x, y;
} Point;
int readPoint(FILE *fp, Point *p);
int readInt(FILE *fp, int *n);
int main(void){
FILE *fp = fopen("instance10_001.txt", "r");
Point p;
int MAX_X, MAX_Y;
readPoint(fp, &p);
MAX_X = p.x;
MAX_Y = p.y;
printf("MAX_X:%d, MAX_Y:%d\n", MAX_X, MAX_Y);
int NUM_PT;
readInt(fp, &NUM_PT);
printf("NUM_PT:%d\n", NUM_PT);
Point arr[NUM_PT];
for(int i = 0; i < NUM_PT; ++i){
readPoint(fp, &arr[i]);
printf("Point(%d, %d)\n", arr[i].x, arr[i].y);
}
fclose(fp);
}
int readLine(FILE *fp, char *buff, int buff_size){
while(fgets(buff, buff_size, fp)){
if(*buff == '#' || *buff == '\n')
continue;
return 1;
}
return 0;
}
#define LINE_MAX 128
int readPoint(FILE *fp, Point *p){
char buff[LINE_MAX];
if(readLine(fp, buff, sizeof buff)){
return 2 == sscanf(buff, "%d %d", &p->x, &p->y);
}
return 0;
}
int readInt(FILE *fp, int *n){
char buff[LINE_MAX];
if(readLine(fp, buff, sizeof buff)){
return 1 == sscanf(buff, "%d", n);
}
return 0;
}
Have made a program that reads a .csv file and stores the highest number in another file. The problem is that my program can't read comma separated numbers like 1,5,6,7,1,2. Here is the loop I need help to change
int i;
int max = 0;
int min = 0;
while (!feof(fp))
{
fscanf( fp, "%d", &i);
if (i < min)
min = i;
if (i > max)
max = i;
}
And this is what I print out:
fprintf(q,"%d",max);
printf("maximum value is %d \n", max);
fclose(q);
fclose(fp);
#include <stdio.h>
#include <limits.h>
int main(void){
FILE *fp = fopen("input.csv", "r");
FILE *q = fopen("max.txt" , "w");
int i;
int max = INT_MIN;
int min = INT_MAX;
while(1){
int state = fscanf(fp, "%d", &i);
if(state == 1){
if (i < min)
min = i;
if (i > max)
max = i;
} else if(state == EOF){
break;
} else {
char ch;
fscanf(fp, " %c", &ch);
if(ch != ','){
fprintf(stderr, "\nformat error\n");
break;
}
}
}
fprintf(q, "%d", max);
printf("maximum value is %d\n", max);
fclose(q);
fclose(fp);
return 0;
}