C++/pointers

Advertisement


Question
QUESTION: 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);
}


Answer
Hi 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

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joydeep Bhattacharya

Expertise

TSR, Socket and Very Complex Mathematical Problem

Experience

Worked on many OOP projects with design patterns

Organizations
http://www.scodz.com

Publications
http://www.scodz.com

Education/Credentials
Master of Computer Applications

Awards and Honors
Being a proud owner of a website of programming called http://www.scodz.com

Past/Present Clients
http://www.funforu.com http://www.dhoondho.com http://analysingc.50webs.com

©2016 About.com. All rights reserved.