THMMY.gr

Μαθήματα Βασικού Κύκλου => Αντικειμενοστραφής Προγραμματισμός => Topic started by: 4Dcube on November 27, 2006, 23:47:44 pm



Title: Βοήθεια!!! στις Προγραμματιστικές τεχνικές Εργασ&#
Post by: 4Dcube on November 27, 2006, 23:47:44 pm
Παραθέτω το μίασμα που έγραψα και που δεν τρέχει καλά και που τα έχω πάρει γιατί δεν βρίσκω για ποιον λόγο
1.βγάζει πρόβλημα όταν εισάγω πίνακα 3χ3 (κάτι παίζει με τη μνήμη ίσως)
2.δε βγάζει καλά αποτελέσματα με τον πίνακα b


Για μια ώρα όποια βοήθεια ευπρόσδεκτη, είμαι πολύ κουρασμένος να ασχοληθώ μαζί του άλλο και πιθανόν να το στείλω έτσι στο χύμα.

*χρησιμοποιώ dev cpp
Code:
#include<iostream>
using namespace std;
#include<stdlib.h>
#include<process.h>

class A{
int n;
int **a;
int *b;
void in_data(void);
void calk(void);
void print_b(void);
public:
       A(void);
       ~A(void);
void set_a(void);
void set_b(void);
void finish_b(void);
};

void A::in_data(void)
{
int i,j;

cout<<"Insert n\n";
cin>>n;

if((a=(int **)malloc(n*n*sizeof(int)))==NULL)
{
cout<<"Not enough memory\n";
exit(1);
}


cout<<"Insert array a\n";

for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
cin>>a[j][i];
}

if((b=(int *)malloc(n*sizeof(int)))==NULL)
{
cout<<"Not enough memory\n";
exit(1);
}
for(j=0;j<n;j++)
{
b[j]=0;
}


}

void A::calk(void)
{
     int i,j;
     
     for(j=0;j<n;j++)
{
                    if(((j+3)%2)==0)
                    {
                                  for(i=0;i<n;i++){if(((a[j][i]+2)%2)==0)b[j]+=a[j][i];}                               
                    }
                    else
                    {
                        for(i=0;i<n;i++){if(((a[j][i]+2)%2)!=0)b[j]+=a[j][i];}           
                    }
    }                 

}

A::A(void)
{
          /*constructor*/
}
A::~A(void)
{
          /*destructor*/
}

void A::set_a(void)
{
A::in_data();
}

void A::set_b(void)
{
A::calk();
}

void A::print_b(void)
{
int i;

cout<<"Array b:\n";

for(i=0;i<n;i++)
cout<<b[i]<<"\n";
}

void A::finish_b(void)
{
A::print_b();
}

void end_program()
{
     char sth;
     cout<<"\n\n\n############################################################################";
     cout<<"\n############################################################################\n";
     cout<<"\n\n\nPress any key to end this program\n\n";
     cin>>sth;
     exit(1);
}

main()
{
A Object;

Object.set_a();
Object.set_b();
Object.finish_b();
end_program();
}


Title: Re: Βοήθεια!!! στις Προγραμματιστικές τεχνικές Εργασία D
Post by: Junior on November 28, 2006, 00:46:50 am
  for(j=0;j<n;j++)
   {
      for(i=0;i<n;i++)
         cin>>a[j];
   }

Αυτό που γράφεις δεν έχει καθόλου νόημα!

Για κάθε στοιχείο του πίνακα a φτιάξε έναν καινούριο πίνακα (στον οποίο θα δείχνει ο pointer που υπάρχει στο αντίστοιχο στοιχείο του a) και στους καινούριους πίνακες πέρνα τους αριθμούς. Ελπίζω να προλαβαίνεις...


Title: Re: Βοήθεια!!! στις Προγραμματιστικές τεχνικές Εργα`
Post by: 4Dcube on November 28, 2006, 01:02:39 am
Εχχ
  for(j=0;j<n;j++)
   {
      for(i=0;i<n;i++)
         cin>>a[j];
   }

Αυτό που γράφεις δεν έχει καθόλου νόημα!
Βασικά δεν ξέρω τι έγινε και αυτό που κουοτάρεις (δλδ αυτό που φαίνεται στο μπορντ με απλό cοpy paste του κώδικα) δεν είναι αυτό που έχω στον κώδικά μου! Έβαλα μετά το code tag. Εξακολουθεί να είναι λάθος?


Δεκτές οι επισημάνσεις και κατόπιν εορτής! Χαλαρά...


Title: Re: Βοήθεια!!! στις Προγραμματιστικές τεχνικές Εργασ&#
Post by: Castrillon on November 28, 2006, 01:14:20 am
Έχει δίκιο ο Junior! Πρέπει να φτιάξεις μία δεύτερη malloc που να δηλώνεις τον pointer int *a ως πίνακα μίας στήλης... (Στα λέω με τον μπακαλίστικο τρόπο αλλά με αυτόν τον τρόπο μαθαίνεις καλή C++...) 8) 8) 8)


Title: Re: Βοήθεια!!! στις Προγραμματιστικές τεχνικές Εργασ&#
Post by: Junior on November 28, 2006, 18:38:16 pm
Εξακολουθεί να είναι λάθος αυτό που έκανες Ηλία...
Κι εγώ το ίδιο λάθος έκανα, αλλά ευτυχώς βρήκα κάποιον γρήγορα και μου το εξήγησε :P


Title: Re: Βοήθεια!!! στις Προγραμματιστικές τεχνικές Εργασ&#
Post by: meltemi on November 28, 2006, 19:20:55 pm

Κι εγώ το ίδιο λάθος έκανα, αλλά ευτυχώς βρήκα κάποιον γρήγορα και μου το εξήγησε :P

Kι εγώ ευτυχώς βρήκα έναν εξίσου καλό..   :P

though not really on time  :-[


Title: Re: Βοήθεια!!! στις Προγραμματιστικές τεχνικές Εργασ&#
Post by: MARIOS on December 21, 2006, 16:07:13 pm
Έχω την Dev c++ και πάω να χρησιμοποιήσω την iostream.h  γιια τις cout και cin, αλλα μου βγάζει ένα μύνημα και με στέλνει σε ένα κείμενο το οποίο δεν καταλαβαίνω!!!!


//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

// As a special exception, you may use this file as part of a free software
// library without restriction.  Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License.  This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.

#ifndef _BACKWARD_BACKWARD_WARNING_H
#define _BACKWARD_BACKWARD_WARNING_H 1

#ifdef __DEPRECATED
#warning This file includes at least one deprecated or antiquated header. \
Please consider using one of the 32 headers found in section 17.4.1.2 of the \
C++ standard. Examples include substituting the <X> header for the <X.h> \
header for C++ includes, or <iostream> instead of the deprecated header \
<iostream.h>. To disable this warning use -Wno-deprecated.
#endif

#endif



PLEASE HELP MEEEEEEEEEE!!!!!!!!!!!!!!!!!!!!!