sour kreme
Fanatic
Mga master dyan, pahelp naman po sa activity namin sa C++
Laitin nyo na po code ko pero gumagawa po kasi kami code ngayon sa dynamic memory allocation namin na lesson. bale ang problema ko po ay may menu po or option dapat kami na dapat maprint or madisplay namin yung previous data na iniput namin tapos dapat may option din na ma free yung isang specific na pointer. medyo mahina pa po sa coding kaya need ko sana tulong nyo mga master. ang problema ko is yung huling data lang po na na input ko yung nadidisplay ko pati narin na ffree. eto po yung code ko.
open po ako sa criticism, sana po matulungan nyo ako
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(){
int an, n, i, k, count=1, p = 0, z, fry = 0;
char run, a, re;
int *cal, *mal;
bool no;
int cal1;
do {
system ("CLS");
int choice;
cout<<"***********************"<<endl;
cout<<"Dynamic Memory Allocation"<<endl;
cout<<"***********************"<<endl;
cout<<"[1] calloc"<<endl;
cout<<"[2] malloc"<<endl;
cout<<"[3] free"<<endl;
cout<<"[4] realloc"<<endl;
cout<<"[5] display all values"<<endl;
cout<<"[6] exit"<<endl;
cout<<"***********************"<<endl;
cout<<"Pick your choice : ";
cin>>choice;
if (choice == 1){
do
{
system("CLS");
cout<<"***********************"<<endl;
cout<<"Calloc ("<<count<<")"<<endl;
cout<<"***********************"<<endl;
cout << "\nSize of allocation? ";
cin >> n;
cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
cal = (int*)calloc(n, sizeof(int));
if (n <= 0)
{
printf("\nMemory not allocated...");
}
else
{
printf("\nMemory Allocation using calloc.\n");
printf("Initializing values are.\n");
int odd=0, even=0, zero=0, min=0;
for (i=0; i<n; i++)
{
cout << "Input data "<<i+1<<" to allocate: ";
cin >> cal1;
no= cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
cal1 = *cal;
if (no == true) {
cout<<"\n Restart the menu";
break;
}
if (cal == 0)
{
zero++;
}
if (cal < 0)
{
min++;
}
if (cal %2 == 0 && cal > 0)
{
even++;
}
if (cal %1 == 0 && cal %2 > 0)
{
odd++;
}
}
printf("\nInitialized values are.\n");
for (i=0; i<n; i++)
{
cout <<"["<<i+1<<"]"<< cal << ", ";
}
cout << "\n\nCount of zero number: "<<zero;
cout << "\nCount of negative number: "<<min;
cout << "\nCount of odd number: "<<odd;
cout << "\nCount of even number: "<<even;
count++;
an++;
z = 1;
cout<<"\nDo you want to allocate another data? [Y/N]";
cin>>a;
}
} while (a == 'Y' || a == 'y');
count = 1;
fry++;
do
{
cout<<"\nDo you want to reallocate the data? [Y/N]";
cin>>re;
cout<<"\n*************************"<<endl;
cout << "REALLOCATION ";
cout << "\n\nNew Size of allocation? ";
cin >> n;
cal = (int*)calloc(n, sizeof(int));
if (n <= 0)
{
printf("\nMemory not allocated...");
}
else
{
printf("\nMemory Allocation using calloc.\n");
printf("Initializing values are.\n");
int odd=0, even=0, zero=0, min=0;
for (i=0; i<n; i++)
{
cout << "Input data "<<i+1<<" to allocate: ";
cin >> cal;
no= cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
if (no == true) {
cout<<"\n Restart the menu";
break;
}
}
printf("\nInitialized values are.\n");
for (i=0; i<n; i++)
{
cout <<"["<<i+1<<"]"<< cal << ", ";
}
cout<< "\n\nMemory Successfully Reallocated";
break;
}
} while (re == 'y' || re =='Y');
}
else if (choice == 2){
do
{
system("CLS");
cout<<"***********************"<<endl;
cout<<"Malloc ("<<count<<")"<<endl;
cout<<"***********************"<<endl;
cout << "\nSize of allocation? ";
cin >> n;
cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
mal = (int*)calloc(n, sizeof(int));
if (n <= 0)
{
printf("\nMemory not allocated...");
}
else
{
printf("\nMemory Allocation using malloc.\n");
printf("Initializing values are.\n");
int odd=0, even=0, zero=0, min=0;
for (i=0; i<n; i++)
{
cout << "Input data "<<i+1<<" to allocate: ";
cin >> cal;
no= cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
if (no == true) {
cout<<"\n Restart the menu";
break;
}
if (cal == 0)
{
zero++;
}
if (cal < 0)
{
min++;
}
if (cal %2 == 0 && cal > 0)
{
even++;
}
if (cal %1 == 0 && cal %2 > 0)
{
odd++;
}
}
printf("\nInitialized values are.\n");
for (i=0; i<n; i++)
{
cout <<"["<<i+1<<"]"<< cal << ", ";
}
cout << "\n\nCount of zero number: "<<zero;
cout << "\nCount of negative number: "<<min;
cout << "\nCount of odd number: "<<odd;
cout << "\nCount of even number: "<<even;
count++;
an++;
z = 1;
cout<<"\nDo you want to allocate another data? [Y/N]";
cin>>a;
}
} while (a == 'Y' || a == 'y');
count = 1;
fry++;
do
{
cout<<"\nDo you want to reallocate the data? [Y/N]";
cin>>re;
cout<<"\n*************************"<<endl;
cout << "REALLOCATION ";
cout << "\n\nNew Size of allocation? ";
cin >> n;
cal = (int*)calloc(n, sizeof(int));
if (n <= 0)
{
printf("\nMemory not allocated...");
}
else
{
printf("\nMemory Allocation using malloc.\n");
printf("Initializing values are.\n");
int odd=0, even=0, zero=0, min=0;
for (i=0; i<n; i++)
{
cout << "Input data "<<i+1<<" to allocate: ";
cin >> cal;
no= cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
if (no == true) {
cout<<"\n Restart the menu";
break;
}
}
printf("\nInitialized values are.\n");
for (i=0; i<n; i++)
{
cout <<"["<<i+1<<"]"<< cal << ", ";
}
cout<< "\n\nMemory Successfully Reallocated";
break;
}
} while (re == 'y' || re =='Y');
}
else if (choice == 3){
if (fry == 0)
{
cout<<"\nFree function will not work without data stored"<<endl;
}
else
{
free(cal);
cout<<"\n freeing the data...\n";
cout<<"\n --> created memory is now free...";
cout<<"\n After declared free function this is the result now :"<<endl;
for (i=0; i <n;++i){
cout<<"["<<i+1<<"] "<<cal<<" "<<endl;
}
if (z == 1 ){
for (i=n; i <p;++i){
cout<<"["<<i+1<<"] "<<mal<<" "<<endl;
a =0;
}
}
fry = 0;
}
}
else if (choice == 4){
char run1;
do //loop start (realloc)
{
system ("CLS");
int c;
cout<<"***********************"<<endl;
cout<<" Realloc"<<endl;
cout<<"***********************"<<endl;
cout<<" [1] calloc"<<endl;
cout<<" [2] malloc"<<endl;
cout<<"***********************"<<endl;
cout<<"\nChoose what type of allocation you want to use: ";
cin>>c;
if (c == 1)
{
cout << "\nSize of allocation? ";
cin >> n;
cal = (int*)calloc(n, sizeof(int));
if (n <= 0)
{
printf("\nMemory not allocated...\n");
}
else
{
printf("\nMemory Allocation using calloc.\n");
printf("Initializing values are.\n");
for (i=0; i<n; i++)
{
cout << "Input data "<<i+1<<" to allocate: ";
cin >> cal;
no= cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
if (no == true) {
cout<<"\n Restart the menu";
break;
}
}
printf("\nInitialized values are.\n");
for (i=0; i<n; i++)
{
cout <<"["<<i+1<<"]"<< cal << ", ";
}
an++;
}
}
else if (c == 2)
{
cout << "\nSize of allocation? ";
cin >> n;
mal = (int*)malloc(n * sizeof(int));
if (n <= 0)
{
printf("\nMemory not allocated...\n");
}
else
{
printf("\nMemory Allocation using malloc.\n");
printf("Initializing values are.\n");
for (i=0; i<n; i++)
{
cout << "Input data "<<i+1<<" to allocate: ";
cin >> mal;
no= cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
if (no == true) {
cout<<"\n Restart the menu";
break;
}
}
printf("\nInitialized values are.\n");
for (i=0; i<n; i++)
{
cout <<"["<<i+1<<"]"<< mal << ", ";
}
an++;
}
}
else
{
printf("\nPlease choose between Option[1] calloc or Option[2] malloc only...\n");
}
cout<<"\nDo you want to run the reallocation option again? [Y/N]";
cin>>run1;
}while (run1 == 'Y' || run1 == 'y');
cout<<"\n*************************"<<endl;
cout << "REALLOCATION ";
cout << "\n\nNew Size of allocation? ";
cin >> n;
cal = (int*)calloc(n, sizeof(int));
if (n <= 0)
{
printf("\nMemory not allocated...");
}
else
{
printf("\nMemory Allocation using calloc.\n");
printf("Initializing values are.\n");
int odd=0, even=0, zero=0, min=0;
for (i=0; i<n; i++)
{
cout << "Input data "<<i+1<<" to allocate: ";
cin >> cal;
no= cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
if (no == true) {
cout<<"\n Restart the menu";
break;
}
}
printf("\nInitialized values are.\n");
for (i=0; i<n; i++)
{
cout <<"["<<i+1<<"]"<< cal << ", ";
}
cout<< "\n\nMemory Successfully Reallocated";
}
}
else if (choice == 5)
{
if (fry == 0)
{
printf("There is no data to be displayed");
}
else
{
printf("\nValues will be displayed...\n");
for (i=0; i<n; i++)
{
cout <<"["<<&cal+1<<"]"<< cal <<endl;
}
}
}
else if (choice == 6){
printf("\nTerminating the program ...\n\n");
printf(">>Program successfully terminated<<\n");
exit(0);
}
else {
printf("\nChoose only from the selections above...\n");
}
cout<<"\nDo you want to run the program again? [Y/N]";
cin>>run;
}while (run == 'Y' || run == 'y');
return 0;
}
Laitin nyo na po code ko pero gumagawa po kasi kami code ngayon sa dynamic memory allocation namin na lesson. bale ang problema ko po ay may menu po or option dapat kami na dapat maprint or madisplay namin yung previous data na iniput namin tapos dapat may option din na ma free yung isang specific na pointer. medyo mahina pa po sa coding kaya need ko sana tulong nyo mga master. ang problema ko is yung huling data lang po na na input ko yung nadidisplay ko pati narin na ffree. eto po yung code ko.
open po ako sa criticism, sana po matulungan nyo ako
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(){
int an, n, i, k, count=1, p = 0, z, fry = 0;
char run, a, re;
int *cal, *mal;
bool no;
int cal1;
do {
system ("CLS");
int choice;
cout<<"***********************"<<endl;
cout<<"Dynamic Memory Allocation"<<endl;
cout<<"***********************"<<endl;
cout<<"[1] calloc"<<endl;
cout<<"[2] malloc"<<endl;
cout<<"[3] free"<<endl;
cout<<"[4] realloc"<<endl;
cout<<"[5] display all values"<<endl;
cout<<"[6] exit"<<endl;
cout<<"***********************"<<endl;
cout<<"Pick your choice : ";
cin>>choice;
if (choice == 1){
do
{
system("CLS");
cout<<"***********************"<<endl;
cout<<"Calloc ("<<count<<")"<<endl;
cout<<"***********************"<<endl;
cout << "\nSize of allocation? ";
cin >> n;
cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
cal = (int*)calloc(n, sizeof(int));
if (n <= 0)
{
printf("\nMemory not allocated...");
}
else
{
printf("\nMemory Allocation using calloc.\n");
printf("Initializing values are.\n");
int odd=0, even=0, zero=0, min=0;
for (i=0; i<n; i++)
{
cout << "Input data "<<i+1<<" to allocate: ";
cin >> cal1;
no= cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
cal1 = *cal;
if (no == true) {
cout<<"\n Restart the menu";
break;
}
if (cal == 0)
{
zero++;
}
if (cal < 0)
{
min++;
}
if (cal %2 == 0 && cal > 0)
{
even++;
}
if (cal %1 == 0 && cal %2 > 0)
{
odd++;
}
}
printf("\nInitialized values are.\n");
for (i=0; i<n; i++)
{
cout <<"["<<i+1<<"]"<< cal << ", ";
}
cout << "\n\nCount of zero number: "<<zero;
cout << "\nCount of negative number: "<<min;
cout << "\nCount of odd number: "<<odd;
cout << "\nCount of even number: "<<even;
count++;
an++;
z = 1;
cout<<"\nDo you want to allocate another data? [Y/N]";
cin>>a;
}
} while (a == 'Y' || a == 'y');
count = 1;
fry++;
do
{
cout<<"\nDo you want to reallocate the data? [Y/N]";
cin>>re;
cout<<"\n*************************"<<endl;
cout << "REALLOCATION ";
cout << "\n\nNew Size of allocation? ";
cin >> n;
cal = (int*)calloc(n, sizeof(int));
if (n <= 0)
{
printf("\nMemory not allocated...");
}
else
{
printf("\nMemory Allocation using calloc.\n");
printf("Initializing values are.\n");
int odd=0, even=0, zero=0, min=0;
for (i=0; i<n; i++)
{
cout << "Input data "<<i+1<<" to allocate: ";
cin >> cal;
no= cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
if (no == true) {
cout<<"\n Restart the menu";
break;
}
}
printf("\nInitialized values are.\n");
for (i=0; i<n; i++)
{
cout <<"["<<i+1<<"]"<< cal << ", ";
}
cout<< "\n\nMemory Successfully Reallocated";
break;
}
} while (re == 'y' || re =='Y');
}
else if (choice == 2){
do
{
system("CLS");
cout<<"***********************"<<endl;
cout<<"Malloc ("<<count<<")"<<endl;
cout<<"***********************"<<endl;
cout << "\nSize of allocation? ";
cin >> n;
cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
mal = (int*)calloc(n, sizeof(int));
if (n <= 0)
{
printf("\nMemory not allocated...");
}
else
{
printf("\nMemory Allocation using malloc.\n");
printf("Initializing values are.\n");
int odd=0, even=0, zero=0, min=0;
for (i=0; i<n; i++)
{
cout << "Input data "<<i+1<<" to allocate: ";
cin >> cal;
no= cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
if (no == true) {
cout<<"\n Restart the menu";
break;
}
if (cal == 0)
{
zero++;
}
if (cal < 0)
{
min++;
}
if (cal %2 == 0 && cal > 0)
{
even++;
}
if (cal %1 == 0 && cal %2 > 0)
{
odd++;
}
}
printf("\nInitialized values are.\n");
for (i=0; i<n; i++)
{
cout <<"["<<i+1<<"]"<< cal << ", ";
}
cout << "\n\nCount of zero number: "<<zero;
cout << "\nCount of negative number: "<<min;
cout << "\nCount of odd number: "<<odd;
cout << "\nCount of even number: "<<even;
count++;
an++;
z = 1;
cout<<"\nDo you want to allocate another data? [Y/N]";
cin>>a;
}
} while (a == 'Y' || a == 'y');
count = 1;
fry++;
do
{
cout<<"\nDo you want to reallocate the data? [Y/N]";
cin>>re;
cout<<"\n*************************"<<endl;
cout << "REALLOCATION ";
cout << "\n\nNew Size of allocation? ";
cin >> n;
cal = (int*)calloc(n, sizeof(int));
if (n <= 0)
{
printf("\nMemory not allocated...");
}
else
{
printf("\nMemory Allocation using malloc.\n");
printf("Initializing values are.\n");
int odd=0, even=0, zero=0, min=0;
for (i=0; i<n; i++)
{
cout << "Input data "<<i+1<<" to allocate: ";
cin >> cal;
no= cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
if (no == true) {
cout<<"\n Restart the menu";
break;
}
}
printf("\nInitialized values are.\n");
for (i=0; i<n; i++)
{
cout <<"["<<i+1<<"]"<< cal << ", ";
}
cout<< "\n\nMemory Successfully Reallocated";
break;
}
} while (re == 'y' || re =='Y');
}
else if (choice == 3){
if (fry == 0)
{
cout<<"\nFree function will not work without data stored"<<endl;
}
else
{
free(cal);
cout<<"\n freeing the data...\n";
cout<<"\n --> created memory is now free...";
cout<<"\n After declared free function this is the result now :"<<endl;
for (i=0; i <n;++i){
cout<<"["<<i+1<<"] "<<cal<<" "<<endl;
}
if (z == 1 ){
for (i=n; i <p;++i){
cout<<"["<<i+1<<"] "<<mal<<" "<<endl;
a =0;
}
}
fry = 0;
}
}
else if (choice == 4){
char run1;
do //loop start (realloc)
{
system ("CLS");
int c;
cout<<"***********************"<<endl;
cout<<" Realloc"<<endl;
cout<<"***********************"<<endl;
cout<<" [1] calloc"<<endl;
cout<<" [2] malloc"<<endl;
cout<<"***********************"<<endl;
cout<<"\nChoose what type of allocation you want to use: ";
cin>>c;
if (c == 1)
{
cout << "\nSize of allocation? ";
cin >> n;
cal = (int*)calloc(n, sizeof(int));
if (n <= 0)
{
printf("\nMemory not allocated...\n");
}
else
{
printf("\nMemory Allocation using calloc.\n");
printf("Initializing values are.\n");
for (i=0; i<n; i++)
{
cout << "Input data "<<i+1<<" to allocate: ";
cin >> cal;
no= cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
if (no == true) {
cout<<"\n Restart the menu";
break;
}
}
printf("\nInitialized values are.\n");
for (i=0; i<n; i++)
{
cout <<"["<<i+1<<"]"<< cal << ", ";
}
an++;
}
}
else if (c == 2)
{
cout << "\nSize of allocation? ";
cin >> n;
mal = (int*)malloc(n * sizeof(int));
if (n <= 0)
{
printf("\nMemory not allocated...\n");
}
else
{
printf("\nMemory Allocation using malloc.\n");
printf("Initializing values are.\n");
for (i=0; i<n; i++)
{
cout << "Input data "<<i+1<<" to allocate: ";
cin >> mal;
no= cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
if (no == true) {
cout<<"\n Restart the menu";
break;
}
}
printf("\nInitialized values are.\n");
for (i=0; i<n; i++)
{
cout <<"["<<i+1<<"]"<< mal << ", ";
}
an++;
}
}
else
{
printf("\nPlease choose between Option[1] calloc or Option[2] malloc only...\n");
}
cout<<"\nDo you want to run the reallocation option again? [Y/N]";
cin>>run1;
}while (run1 == 'Y' || run1 == 'y');
cout<<"\n*************************"<<endl;
cout << "REALLOCATION ";
cout << "\n\nNew Size of allocation? ";
cin >> n;
cal = (int*)calloc(n, sizeof(int));
if (n <= 0)
{
printf("\nMemory not allocated...");
}
else
{
printf("\nMemory Allocation using calloc.\n");
printf("Initializing values are.\n");
int odd=0, even=0, zero=0, min=0;
for (i=0; i<n; i++)
{
cout << "Input data "<<i+1<<" to allocate: ";
cin >> cal;
no= cin.fail();
cin.clear() ;
cin.ignore(INT_MAX,'\n');
if (no == true) {
cout<<"\n Restart the menu";
break;
}
}
printf("\nInitialized values are.\n");
for (i=0; i<n; i++)
{
cout <<"["<<i+1<<"]"<< cal << ", ";
}
cout<< "\n\nMemory Successfully Reallocated";
}
}
else if (choice == 5)
{
if (fry == 0)
{
printf("There is no data to be displayed");
}
else
{
printf("\nValues will be displayed...\n");
for (i=0; i<n; i++)
{
cout <<"["<<&cal+1<<"]"<< cal <<endl;
}
}
}
else if (choice == 6){
printf("\nTerminating the program ...\n\n");
printf(">>Program successfully terminated<<\n");
exit(0);
}
else {
printf("\nChoose only from the selections above...\n");
}
cout<<"\nDo you want to run the program again? [Y/N]";
cin>>run;
}while (run == 'Y' || run == 'y');
return 0;
}