C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 2nd, 2012, 04:00 AM
chrislad chrislad is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 6 chrislad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 48 m 23 sec
Reputation Power: 0
C - Using Malloc

I'm trying to copy from a file. The structure of the file is the first line is 2 ints (to give the size needed for storing the remaining floats in memory) and a list of floats.

I am using findvals -r float -f tolerance < mat.10x20 > output

The user should be able to specify a float, and a tolerance, and the program should compare to the file and output the number of valid numbers to another file. Just doesn't seem to be working and I'm stumped! Would be so much easier in Java!!

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"

int main(int argc, char* argv[])
{

 if (strcmp(argv[1],"-r") != 0 || strcmp(argv[3],"-f") != 0)
	{
	 perror(argv[1]);
   	 exit(1);	
	}

 else if (strcmp(argv[3],"-f") != 0 || strcmp(argv[1],"-r") != 0)
	{
	 perror(argv[3]);
	 exit(1);
	}

else
	{
		
	}



  int rcount, ccount; //number of rows and columns.

  float referenceNum = strtof(argv[1],0); // defines the number looking for
  float toleranceNum = strtof(argv[2],0); // defines the tolerance accepted

  scanf("%d %d", &rcount, &ccount);



  float** rows = malloc(rcount * sizeof(float *));
	if (rows == 0)
	{
		fprintf(stderr, "Couldn't alocate sufficient space.\n");
		exit(1);
	}
  
  int i;
	for (i = 0; i < rcount; i++)
	{
		float* row = malloc(ccount * sizeof(float));
			if (row == 0)
				{
					fprintf(stderr, "Couldn't alocate sufficient row space.\n");
					exit(1);
				}
			rows[i] = row;
	}


int x, y;

for (x = 0; x<rcount; x++)
{
	for (y = 0; y<ccount; y++)
	{
		scanf("%f", &rows[x][y]);
	}
}

for (x = 0; x<rcount; x++)
{
	for (y = 0; y<ccount; y++)
	{
		printf("%f", rows[x][y]);
	}
}
  exit(0);
}


This would be mat.2x3

2 3
68.673767 11.473727 87.138351
51.231050 -25.041696 -33.239058

Reply With Quote
  #2  
Old November 2nd, 2012, 04:01 AM
chrislad chrislad is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 6 chrislad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 48 m 23 sec
Reputation Power: 0
Oh, I don't have the code implemented for comparison yet, and I don't think I'm getting it into memory correctly as the printf should be putting the whole file into an output file without checking, but that doesn't seem to be working.

Reply With Quote
  #3  
Old November 2nd, 2012, 04:32 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 19 m 34 sec
Reputation Power: 1774
There is nothing wrong with the malloc code, but the logic for testing argv parameters needs work.

> if (strcmp(argv[1],"-r") != 0 || strcmp(argv[3],"-f") != 0)
If argv[1] is "-r", then the left side evaluates to false, and the right side evaluates to true.
false || true is true.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C - Using Malloc

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap