segmentation fault HELP

This is a discussion on segmentation fault HELP within the C Programming forums, part of the General Programming Boards category; I wrote this program and it compiled with no errors or warnings but when i went to run it i ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    segmentation fault HELP

    I wrote this program and it compiled with no errors or warnings but when i went to run it i get a segmentation fault and i cant figure out what is causing it. Ive spent hours and hours on this . Anyone with some expertise willing to take a look?
    Code:
    1. }
    #include <stdio.h>#include "my.h"int main(int argc, char* argv[]){int **a;int rows, cols,j,k;FILE* p;int i;fopen(argv[1], "r");fscanf( p, "%d", &rows);fscanf( p, "%d", &cols);printf("works here");a = (int**)calloc (rows, sizeof(int*));for (i = 0; i <rows; i++){ a[i] = (int*)calloc (cols, sizeof(int));}for (j=0;j<rows; j++){ for(k = 0; k < cols; k++) { fscanf(p, "%d", &a[j][k]); }}printme(a,rows, cols);oddvalues(a,rows, cols);oddlocations(a, rows, cols);countoddrows(a,rows,cols);addrows(a,rows,cols);findfirstsmall(a,rows,cols);findlastlarge(a,rows,cols);addcolumns(a,rows,cols);averagecolumns(a,rows,cols);free(a);return 0 ;}HEADER:#include <stdio.h>#define my#include <stdbool.h>#include <limits.h>#include <stdlib.h>void printme(int** a, int rows, int cols);void oddvalues(int** a, int rows, int cols);void oddlocations(int** a, int rows, int cols);void countoddrows(int** a, int rows, int cols);void addrows(int** a, int rows, int cols);void findfirstsmall(int** a, int rows, int cols);void findlastlarge(int** a, int rows, int cols);void addcolumns(int** a, int rows, int cols);void averagecolumns(int** a, int rows, int cols);PRINTME.c:#include "my.h"#include <stdio.h>void printme(int** a, int rows, int cols){int i;int j;printf("a. The contents of the array are the ints, printed row by row:..................");for (i = 0; i<rows; i++){ for(j=0; j<cols; j++) { printf("%3d", a[i][j]); } printf("\n");}return;}ODDVALUES.c#include "my.h"#include <stdio.h>void oddvalues(int** a, int rows, int cols){int i;int j;int even = 0;printf("b. The zeroed elements and odds of the array are: ...................");for(i = 0; i<rows; i++){ for(j=0; j<cols; j++) { if(a[i][j]%2 !=0) { printf("%3d", a[i][j]); } else { printf("%3d", even); } }}return ;}ODDLOCATIONS:#include "my.h"#include <stdio.h>void oddlocations(int** a, int rows, int cols){printf("\n");printf("c. The various rows of the array that contain at least one odd int are:.......................");int i;int j;for(i = 0; i<rows; i++){ for(j=0; j<cols; j++) { if(a[i][j]%2 !=0); { printf("%3d", a[i]); } }}return ;}COUNTODDROWS:#include "my.h"#include <stdio.h>void countoddrows(int** a, int rows, int cols){int i;int j;int count = 0;printf("\n");printf("d. The various number of odd numbers in each row is:..................");for(i = 0; i<rows; i++){ count = 0; for(j=0; j<cols; j++) { if(a[i][j]%2 !=0) { count++; } printf("%3d", count); }}return ;}ADDROWS:#include "my.h"#include <stdio.h>void addrows(int** a , int rows, int cols){int i;int j;printf("\n");printf("e. The various row sums are:..................");for(i = 0; i<rows; i++){ int temp = 0; int sum = 0; for(j=0; j<cols; j++) { temp = a[i][j]; sum = sum + temp; } printf("%3d", sum);}return ;}#include "my.h"#include <stdio.h>FINDFIRSTSMALL:void findfirstsmall(int** a, int rows, int cols){int i;int j;int small;printf("\n");printf("f. The various first indices of the smallest int for each and every row is:.............");for(i = 0; i<rows; i++){ small = INT_MAX; for(j=0; j<cols; j++) { if (a[i][j] < small) { small = j; } } printf("%3d", small);}return ;}FINDLASTLARGE:#include "my.h"#include <stdio.h>void findlastlarge(int** a, int rows, int cols){int i;int j;int large;printf("\n");printf("g. The various last indices of the largest int for each and every row is...........");for(i = 0; i<rows; i++){ large = INT_MIN; for(j=0; j<cols; j++) { if (a[i][j] >= large) { large = j; } } printf("%3d", large);}return ;}ADDCOLUMNS:#include "my.h"#include <stdio.h>void addcolumns(int** a, int rows, int cols){int i;int j;int sum = 0;int temp;printf("\n");printf("h. The various column sums are:");for(i = 0; i<cols; i++){ temp = 0; sum = 0; for(j=0; j<rows; j++) { temp = a[i][j]; sum = sum + temp; } printf("%3d", sum);}return ;}AVERAGECOLUMNS:#include "my.h"#include <stdio.h>void averagecolumns(int** a, int rows, int cols){int i;int j;int sum = 0;int temp;double average;printf("\n");printf("i. The various column averages are:");for(i = 0; i<cols; i++){ temp = 0; average = 0; sum = 0; for(j=0; j<rows; j++) { temp = a[i][j]; sum = sum + temp; average = sum / rows; } printf("%3.2lf", average);}return ;}

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Care to reformat it so a human being can read it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault - IPC
    By kbfirebreather in forum C Programming
    Replies: 7
    Last Post: 02-01-2009, 02:17 PM
  2. Segmentation fault
    By gkoenig in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 01:14 PM
  3. segmentation fault!!
    By Necromancer in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 07:03 AM
  4. Segmentation fault, I can't see why?
    By micke_b in forum C Programming
    Replies: 2
    Last Post: 11-07-2007, 08:47 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21