C++/pointers
Expert: Joydeep Bhattacharya - 9/17/2009
QuestionQUESTION: Hi Joydeep..
can you help me answer with this please..
array of student score declared as dynamic data
-declared a dynamic data array to contain the scores for 10 students.then step through the array assigning the counter as the value of the score for that array element.finally,write another for loop that displays each score.remember that you have to use pointer to access the array.you can do this in three steps:
1.)declare a pointer and allocate space for your array
int *arr,new int[size];
2.)use a for loop to step through the array (using either pointer or the subscript notation)
for(int i=0;i<size;i++)
{
arr[i]=i;
}
3.)finally,use another for to display the score
for(int i=0;i<size;i++)
{
cout<<"score for student"<<i+1":"
<<*(arr+i)<<endl;
}
NOTE: dont forget to use delete to free up space used by the array
hope you can help me with this its very important!!
thank in advance Joydeep..
ANSWER: Hi Sean
How are you ... hope that you are doing good
Check this code
#include<stdio.h>
#include<alloc.h>
void main(){
int *arr, size, i;
printf("Enter the number of students: ");
scanf("%d",&size);
//creating dynamic array of user defined size
arr = (int*)malloc(sizeof(int)*size);
printf("ENTER DATA \n");
for(i = 0 ; i < size ; i++){
//getting the data for the number specified
printf("Enter data for %d student: ",i+1);
scanf("%d",arr + i);
}
printf("\nDISPLAY DATA\n\n");
for(i = 0 ; i < size ; i++){
//displaying the data
printf("Data for %d student: %d\n",i+1,*(arr + i));
}
//deallocating dynamic array
free(arr);
}
In case of any problem with this program please feel free to get back to me. You can also check my website for many such programs.
regards
Joydeep Bhattacharya
http://www.scodz.com
---------- FOLLOW-UP ----------
QUESTION: hello Joydeep!
its me again sean..im doing fine here..anyway thank for the answer since its help me.
i have another question..
can you please convert this code into c++?? thank again.
#include<stdio.h>
#include<alloc.h>
void main(){
int *arr, size, i;
printf("Enter the number of students: ");
scanf("%d",&size);
//creating dynamic array of user defined size
arr = (int*)malloc(sizeof(int)*size);
printf("ENTER DATA
");
for(i = 0 ; i < size ; i++){
//getting the data for the number specified
printf("Enter data for %d student: ",i+1);
scanf("%d",arr + i);
}
printf("
DISPLAY DATA
");
for(i = 0 ; i < size ; i++){
//displaying the data
printf("Data for %d student: %d
",i+1,*(arr + i));
}
//deallocating dynamic array
free(arr);
}
AnswerHi Sean
The same code converted from C to C++
#include<iostream.h>
class Student{
private:
int marks;
public:
void setMarks(int marks){
this->marks = marks;
}
int getMarks(){
return this->marks;
}
};
void main(){
int size, i, temp;
Student *arr = NULL;
cout<<"Enter the number of students: ";
cin>>size;
arr = new Student[size];
cout<<"ENTER DATA \n";
for(i = 0 ; i < size ; i++){
cout<<"Enter data for "<<i + 1<<" student: ";
cin>>temp;
arr[i].setMarks(temp);
}
cout<<"\nDISPLAY DATA\n\n";
for(i = 0 ; i < size ; i++){
cout<<"Data for "<<i + 1<<" student "<<arr[i].getMarks()<<"\n";
}
delete arr;//deallocating dynamic array
}
In case of any problem please feel free to get back to me ...
You can also check my personal website for more such programs at
http://www.scodz.com/
regards
Joydeep Bhattacharya
http://www.scodz.com