gerard4143 371 Nearly a Posting Maven

Yeah it works, but your using memory that you freed which is a no no.

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double **memalloc(int M,int N);
int main()
{
  int i,j;  
  double **a;
  a=memalloc(2,2);
  printf("a\n");
  for(i=0;i<2;i++)
    {
      printf("\n");
      for(j=0;j<2;j++)
	{
	  a[i][j]=i+j;
	  printf("%f\t",a[i][j]);	  
	}
    }
	/*free the additional memory here*/
  free(a);

	return 0;
}

double **memalloc(int M,int N)
{
  int i;
  double **y;
  y = malloc(M* sizeof(double *));
  if(y == NULL)
    {
      printf("out of memory\n");
      return 0;
    }
  for(i = 0; i < M; i++)
    {
      y[i] = malloc(N * sizeof(double));
      if(y[i] == NULL)
	{
	  printf("out of memory\n");
	  return 0;
	}
      free(y[i]);/*don't free the memory here*/	  
    } 

  return y;
}
gerard4143 371 Nearly a Posting Maven

Wow, you have functions that are +100 lines long... This would be a good time to consider using the inline qualifier and break these large functions into smaller, neater inline functions.

gerard4143 371 Nearly a Posting Maven

Thanks I'll check that out.

gerard4143 371 Nearly a Posting Maven

Hi,

I'm looking for a good book on Qt 4 programming, I checked both chapters and amazon but couldn't locate a Qt book with favorable reviews and was wondering if the daniweb members knew of a good text...Thanks in advance. Gerard4143

gerard4143 371 Nearly a Posting Maven

any help?

Any help? I told you what's wrong with your code. Correct the problems and your code will work.

gerard4143 371 Nearly a Posting Maven

Inclusion guards are a good example of usage.

http://en.wikipedia.org/wiki/Include_guard

gerard4143 371 Nearly a Posting Maven

Please look at the code below

#include <stdio.h>

int main()
{
	int total = 60;
	char gradeLetter1 = 'A';
	char gradeLetter2 = 'B';
	char gradeLetter3 = 'C';
	char gradeLetter4 = 'D';

	if((total >= 90) && (total <= 100)) 
	{
		printf("%c          \n ", gradeLetter1 );
	}
	else if((total >= 80) && (total <= 89)) 
	{ 
		printf("%c \n", gradeLetter2);
	}
	else if((total >= 70) && (total <= 79)) 
	{
		printf("%c \n", gradeLetter3);/*c% should be %c*/
	}
	else if((total >= 60) && (total <= 69)) 
	{
		printf("%c \n", gradeLetter4);/*c% should be %c*/
	}
	else if(total <= 59) 
	{
		printf("You have Failed with a %d percent \n", total);
	}
	return 0;
}

or better

#include <stdio.h>

int main()
{
	int total = 55;
	char gradeLetter1 = 'A';
	char gradeLetter2 = 'B';
	char gradeLetter3 = 'C';
	char gradeLetter4 = 'D';

	if(total >= 90) 
	{
		printf("%c          \n ", gradeLetter1 );
	}
	else if(total >= 80) 
	{ 
		printf("%c \n", gradeLetter2);
	}
	else if(total >= 70) 
	{
		printf("%c \n", gradeLetter3);/*c% should be %c*/
	}
	else if(total >= 60) 
	{
		printf("%c \n", gradeLetter4);/*c% should be %c*/
	}
	else if(total <= 59) 
	{
		printf("You have Failed with a %d percent \n", total);
	}
	return 0;
}
gerard4143 371 Nearly a Posting Maven

First question, what do you hope to gain by casting this?

if((int) total >= 90 && total <= 100)
gerard4143 371 Nearly a Posting Maven

Yes you need the libraries statically compiled or to be present on the machine.

gerard4143 371 Nearly a Posting Maven

When you need dynamic memory, trees, lists, passing large objects to functions.

gerard4143 371 Nearly a Posting Maven

Your original posting never mentioned anything about static source code.

gerard4143 371 Nearly a Posting Maven

Try this and you'll display the first element in imgSet.

printf("%c", imgSet[0] );
gerard4143 371 Nearly a Posting Maven

The only difference I can see is, if you use the second method you have to open and update the header.h file whenever you need a new libraries functionality..The first method, just add the include at the top of the file no updating a header file.

gerard4143 371 Nearly a Posting Maven

I'm no math expert but

mid = (min + (max - min)) / 2;

doesn't that reduce to

mid = max / 2;
gerard4143 371 Nearly a Posting Maven

Could you post a small example of the datafile.

gerard4143 371 Nearly a Posting Maven

Here's an example that uses enough functionality to answer your questions.

#include <stdio.h>
#include <string.h>

#define ARR_SIZE 100

int main()
{
	size_t i = 0;
	size_t size = 0;
	char str[ARR_SIZE];

	fputs("Enter a string->", stdout);
	fgets(str, ARR_SIZE, stdin);

	size = strlen(str);

	for (i = 0; i < size; ++i)
		fputc(str[i], stdout);
	return 0;
}
gerard4143 371 Nearly a Posting Maven

This forum isn't a homework completion site, its a site to aid coders in the understanding of the computers and programming.

gerard4143 371 Nearly a Posting Maven

The problem is your understanding of a c-string. A c-string is an array of characters terminated by an '\0'. So this

char a;

Is not a c-string, its a character.

gerard4143 371 Nearly a Posting Maven

Your not posting enough information to diagnose your problem.

gerard4143 371 Nearly a Posting Maven

Try using cin instead of gets to get character from the user in countletter function.
As gets is used to get strings and not a single character.

void countletter(char *str)
{
	int count;
	char a;
	cin>>a;
	for (;(*str)!='\0';str++)
	{
		if (*str==a)
		{
		count++;
		}
	}
	 printf("%d",count);
}

You release this is the C section.

gerard4143 371 Nearly a Posting Maven

A pointer on gets()..Here's what my help file states about its use.

Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.

gerard4143 371 Nearly a Posting Maven

Write a function that accepts a pointer to a string and a character and returns the number of times the character is found in the string.

Your function prototype should be

int countletter(char *str, char c);
gerard4143 371 Nearly a Posting Maven

Yes, if you have a multi-core processor then its possible to run both programs at the same time but I'm uncertain of how you would get the operating system to coordinate or make sure that when one is running then run the other...A better solution would be one program that has two threads.

gerard4143 371 Nearly a Posting Maven

Every time you call malloc, you create an independent area of memory that's identified by the pointer returned by malloc. So free'ing a or y only frees the memory allocated here on line 27.

y = malloc(M* sizeof(double *));

Now if you'll check lines 33 - 41 you'll note your calling malloc again in this loop.

for(i = 0; i < M; i++)
    {
      y[i] = malloc(N * sizeof(double));
      if(y[i] == NULL)
	{
	  printf("out of memory\n");
	  return 0;
	}	  
    }

You have to free the memory allocated here as well.

gerard4143 371 Nearly a Posting Maven

It should be (condition && condition) not (condition || condition). All grades(except 100) will be less than 100 so your first condition will always be true.

gerard4143 371 Nearly a Posting Maven

You can only have one main function in an executable.

gerard4143 371 Nearly a Posting Maven

No, the reason to include header files in a make file is so that if the header file is updated, but the source file that includes it has not been updated, that source file is recompiled anyhow.

Good point.

gerard4143 371 Nearly a Posting Maven

Right away I see a problem...What if you enter 90 or 100, how does your program handle that. You should be saying, A is a grade from 90 to 100.

if (total >= 90)
gerard4143 371 Nearly a Posting Maven

The only reason to put header files in makefile is to check to see if they exist. Its way of ensuring that you have all the files required and if not producing an error indicating which files are required. The #include "filename.h" is preprocessed therefore writing the header directly into the source code.

gerard4143 371 Nearly a Posting Maven

Then why is it in the list of includes

#include "liste.h"
#include "outils_liste.h"
#include "constantes.h"

Take your make file and reverse every entry except the ones in clean....also

prog2.o: liste.o outils_liste.o constantes.o prog2.c
gcc -c prog2.c

header files are for the preprocessor and source/object files are for the makefile.

gerard4143 371 Nearly a Posting Maven

I would say the entries in your Makefile are in the wrong order.

This line

test: prog2.o
	gcc *.o -o test

requires that

liste.c
outils_liste.c
constantes.c

are built object files. They aren't built when you call them here....unless your make utility reads the entries in the opposite order.

gerard4143 371 Nearly a Posting Maven

It also supports the data type of string.

Then its not C. Why don't you write this in C++?

gerard4143 371 Nearly a Posting Maven

So the code posted below is proper.

#include <iostream>

typedef int(*pfunc1)(int);
typedef int(*pfunc2)(int, int);

int double_it(int x)
{
	return x * 2;
}

int double_it(int x, int y)
{
	return (x + y) * 2;
}


int main(int argc, char**argv)
{
	pfunc1 func_1 = double_it;
	pfunc2 func_2 = double_it;

	std::cout << func_1(123) << std::endl;
	std::cout << func_2(345, 567) << std::endl;
	return 0;
}
gerard4143 371 Nearly a Posting Maven

I'm wondering what is the proper way to point at an overloaded function. Is it correct to cast the function to the proper type?

#include <iostream>

int double_it(int x)
{
	return x * 2;
}

int double_it(int x, int y)
{
	return (x + y) * 2;
}


int main(int argc, char**argv)
{
	void *myfunc = (void*)(int(*)(int))double_it;
	return 0;
}
mike_2000_17 commented: nice question +2
gerard4143 371 Nearly a Posting Maven

Here's a general rule about malloc - The number of times that you call malloc, is the the number of times that you have to call free. Your program creates a memory leak by free'ing a before free'ing the other allocated memory areas.

And your error

free(a);

This was pointed out previously.

When you write programs that allocate and free memory, you should run them with a program like valgrind to ensure your not creating memory leaks.

Also, in C the main function is defined as

int  main()
{
/*...*/
return 0;
}
gerard4143 371 Nearly a Posting Maven

You'll have to elaborate why the engine doesn't support arrays and what exactly it does support...We can't grab a crystal ball and gaze into your game engine.

gerard4143 371 Nearly a Posting Maven

C doesn't have objects. The closest thing is a C-string which is an array of characters terminated by '\0'.

char str1[] = "this is a string";/*compiler adds the '\0'*/
char *str2 = "this is another string";/* but its non mutable and the compiler adds the '\0'"*/
char str3[] = {'s','t','r','i','n','g','\0'};/*another string*/
char str4[]  = {'n','o','n'','-','s','t','r','i','n','g'};/*not a string no '\0'*/
WaltP commented: Good list. Esp adding #4... +15
gerard4143 371 Nearly a Posting Maven

Are you sure this is C? I ask because you have a type string.

gerard4143 371 Nearly a Posting Maven

Try passing your structure by reference....Wow you called your structure std? Don't do that.

#include <iostream>
#include <cstdlib>

using namespace std;

struct str/*don't call your structure std*/
{
     string name;
     int idNumber;
     string year;
     float gradepoint[8];
     int hours[8];
     float avg;
};

void testFill(struct str & arg);/*pass by reference*/
void testPrint(struct str arg);

int main(int argc, char *argv[])
{
	char pause;/*pause execution*/
	struct str student;

	testFill(student);
	testPrint(student);

	std::cin >> pause;
	return EXIT_SUCCESS;
}

void testPrint(struct str arg)
{
     cout << "Name: " << arg.name << endl;
     cout << "Id Number: " << arg.idNumber << endl;
     cout << "Year: " << arg.year << endl;

     for(int i = 0; i < 8; i++)
     {
          cout << "Grade: " << arg.gradepoint[i] << " ";
          cout << "Hours: " << arg.hours[i] << endl; 
     }

     cout << "Average: " << arg.avg << endl;
}

void testFill(struct str & arg){
     int counter = 0;
     float total = 0;

     arg.name = "Bob Bobson";
     arg.idNumber = 123456;
     arg.year = "Freshman";

     for(int i = 0; i < 8; i++)
     {
          arg.gradepoint[i] = i+80;
          arg.hours[i] = 3;
          total = total+arg.gradepoint[i];
          counter++;
     }

     arg.avg = total / counter;

}
gerard4143 371 Nearly a Posting Maven

tried that also. still error persists

Why are you free'ing y and then returning it?

vineeshvs commented: good ans +1
gerard4143 371 Nearly a Posting Maven

Oh thanks a lot Gerad !!!!
Can u show me the final code with the loop ?

How about you show us the final code.

gerard4143 371 Nearly a Posting Maven

How is that your code? That's a reference link to strcat.

gerard4143 371 Nearly a Posting Maven

Could we see an example of the code you tried?

gerard4143 371 Nearly a Posting Maven

For this to work, you'll have to save the number of characters matched in each retrieval... Something like below.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char**argv)
{
	int res = 0;
	int res2 = 0;
	char buf [] = "25+2;44+1;8-2.";

	char op1[20];
	char op2[20];
	char operazione[20];
	char en[20];

	res = sscanf(&buf[res2],"%[0-9] %[+-] %[0-9] %[;.]",op1,operazione,op2,en );
	res2 += res + 1;
	fprintf(stdout, "op1->%s, op2->%s, operazione->%s, en->%s\n", op1, op2, operazione, en);
	res = sscanf(&buf[res2],"%[0-9] %[+-] %[0-9] %[;.]",op1,operazione,op2,en );
	res2 += res + 1;
	fprintf(stdout, "op1->%s, op2->%s, operazione->%s, en->%s\n", op1, op2, operazione, en);
	res = sscanf(&buf[res2],"%[0-9] %[+-] %[0-9] %[;.]",op1,operazione,op2,en );
	fprintf(stdout, "op1->%s, op2->%s, operazione->%s, en->%s\n", op1, op2, operazione, en);

	exit(EXIT_SUCCESS);
}

I would put this functionality in a while loop to clean it up.

gerard4143 371 Nearly a Posting Maven

I know what I meant to imply

and what was that?

gerard4143 371 Nearly a Posting Maven

:icon_rolleyes:

So your stating that memory attributes can't be changed? Are you sure about your 'rolling eyes' and what it implies.

gerard4143 371 Nearly a Posting Maven

Technically speaking, the only difference between code and data are memory attributes. So an array could carry values that equate to a function calls but you could not call them unless you changed the memory attributes to read/execute and pointed the instruction pointer at them..

gerard4143 371 Nearly a Posting Maven

This is how I would solve this problem

#include <iostream>

class BaseClass
{
public:

    BaseClass( ){ }

    ~BaseClass( ){ }

	double getitsvalue() const { return value; }
	void setitsvalue(double val) { value = val; }

protected:

    double value;

};

class DerivedClass : public BaseClass
{
public:

    DerivedClass( ) { }

    ~DerivedClass( ){ }

};

std::ostream& operator <<(std::ostream & out, const BaseClass & b)
{
	return out << b.getitsvalue();
} 

int main( )
{
    DerivedClass myDerivedClass;

    myDerivedClass.setitsvalue(10.0);

    std::cout << myDerivedClass << std::endl;

    return 0;
}

Since DerivedClass objects can be sliced down to BaseClass this'll work.

gerard4143 371 Nearly a Posting Maven


However, the compiler is permitted to optimize away this copy (provided that the relevant copy constructor is accessible, which in this case it is), in which case the two examples may well generate identical code.

Thanks for explaining that..

gerard4143 371 Nearly a Posting Maven

Nothing, you are constructing not assigning.