Monday, May 24, 2010

Please help me to write a c program accept n-input " thoes No is an array elements "?

Write C program which has a function that compute the sum of n number


Note: the n number is the values of array elements.

Please help me to write a c program accept n-input " thoes No is an array elements "?
use for(i=0;i%26lt;n;i++) sum=sum+i;


How to display a 2 dimensional array in columns using C?

To display a table, simply use column and row variables, display each column, and then issue a printf("\n");





To make your columns a fixed length, for example 4, instead of doing a "%d" in the format string do a %4d". If you are doing floats and want say 5 figures of whole numbers and 2 decimal places, "%5.2f".





In other words this is very straightforward.

How to display a 2 dimensional array in columns using C?
for(i=0;i%26lt;rows;i++)


{


for(j=0;j%26lt;columns;j++)


{


printf("\t%d",x[i][j]);


}


printf("\n");


}


C struct array question?

I have an array of structures:


struct city cities[100];





I want to place two "instances" of that struct array into a method that computes a distance:





double compute_distance(city cities[] x, city cities[] y) {





/* some code here */





}








This method is wrong. How do I get it to work?

C struct array question?
If you want to pass two arrays to the function, then change the following line:


double compute_distance(city cities[] x, city cities[] y)





To:


double compute_distance(struct city x[ ], struct city y[ ])








If you want to pass two instanses to the function change the previous line to:





double compute_distance(struct city x, struct city y)





To call the function within main, all you have to do is the following:





double distance = 0.0;


int a = 0; // change this to the index of the 1st. city


int b = 0; // change this to the index of the 2nd city


distance = compute_distance(cities[ a ], cities[ b ]);
Reply:Hi,





You have one array, with 100 "instances" of the city struct in it, so I think you mean that you want to call the compute_distance function with two cities from the array. You could do this just making the parameters of type "struct city", but that would make a copy of each city for the procedure - with two side effects:


1) If you make changes to either city inside the function, they're not made to the copies in the array, and


2) It takes time and memory to make copies.





So I recommend using pointers as the parameters, like this:





double compute_distance(struct city * x, struct city * y)


{


/* some code here */


}





Then you use x-%26gt;some_field style syntax inside the function.





Call the function using the %26amp; operator to get a pointer to a certain city. For example, to call compute_distance with the 5th and 8th cities:





distance = compute_distance(%26amp;cities[4], %26amp;cities[7]);





Good luck!

petal

What is the dev c++ formula that declares an array of 20 integers. Accept a number to determine the.........?

what is the dev c++ formula that declares an array of 20 integers. Accept a number to determine the number of 0 or 1 inputs(binary digits). Display decimal value using the output format:





Enter size: 4


1


0


1


1


Equivalent: 11


1x8=8


0x4=0


1x2=2


1x1=1

What is the dev c++ formula that declares an array of 20 integers. Accept a number to determine the.........?
Hello Edrew !!





Here is your solution:








#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;math.h%26gt;





void main()


{


int num[20]={0};


int total,i;


double sum=0;


clrscr();


cout%26lt;%26lt;"How many digits do you wish to Enter : ";


cin%26gt;%26gt;total;





if(total%26gt;0 %26amp;%26amp; total%26lt;=20)


{


cout%26lt;%26lt;endl%26lt;%26lt;"Please Enter "%26lt;%26lt;total%26lt;%26lt;" digits : "%26lt;%26lt;endl;


for(i=0;i%26lt;total;i++)


{


cin%26gt;%26gt;num[i];


}








for(i=0;i%26lt;total;i++)


{


sum=sum + num[i]*pow(2,total-i-1);


cout%26lt;%26lt;endl%26lt;%26lt;num[i] %26lt;%26lt;"*"%26lt;%26lt;pow(2,total-i-1)%26lt;%26lt;"="%26lt;%26lt;num[i]*pow...


}


cout%26lt;%26lt;endl%26lt;%26lt;endl%26lt;%26lt;"Equivalent is : "%26lt;%26lt;sum;


}


else


cout%26lt;%26lt;"Please Enter value between 0 and 20..";





getch();


}
Reply:Try searching the net for C++ tutorials and resources
Reply:Try searching the net for C++ tutorials and resources. It's worth bearing in mind that Dev C++ is an editor, not a language - the languages it edits are C or C++.


C program to delete duplicate elements in an array?

i want a simple c code to delete duplicate elements in an array

C program to delete duplicate elements in an array?
http://www.google.com/codesearch?q=array...
Reply:assuming your array is x with n elements.





i=0;


while (i%26lt;n-1)


{


j=i+1;


while(j%26lt;n)


{


if (x[i]==x[j])


{


for (int k=j; k%26lt;n-1; k++)


x[k]=x[k+1];





n--


}


j++;


}


i++;


}


How do I remove multiple spaces from a cstring (character string) in c++?

For example


If I have a two dimmensional character array string: songdata[x][y]. and it contains the characters "the great escape". How do I manipulate it to make it look like:


"the great escape"





I just want to remove spaces where there are multiple spaces, but not remove all of the spaces.

How do I remove multiple spaces from a cstring (character string) in c++?
Seasoned programmers will want to howl at me on this code because we're building the replacement string into the same buffer (songlist[x][y]) as the source buffer--(not using a temporary buffer)....generally a bad practice.





However, this code works (and is extremely efficient) because--in this instance--the resulting string is guaranteed to be the same len, or shorter, than the source string. A little thought on this will prove it out.





Best wishes,





_g








void strip_excess_space(int songlist_count)


{


int x=0, y=0, z=0;


int done=0;


int ch, lastCh=' ';





while(!done)


{


ch = songdata[x][y++];


switch(ch)


{


case ' ': //a space?


if(lastCh != ch) //if last character wasn't a space...


songdata[x][z++] = ch; //permit it.


break;





case '\0': //end of string


songdata[x][z++] = ch;


x++; //go to next name in songlist


z=y=0; //...and beginning of buffers


//check for end


if(x %26gt;= songlist_count)


done = 1;


break;





default:


songdata[x][z++] = ch;


break;


}


lastCh =ch;


}


}


C++ dynamic array function help?

Write a program that will keep track of the enrollment of one class. Use a dynamic array to keep track of the enrollment. When enrolling a new student, the dynamic array will increase by one. You must use a function call to add student. I have been working on this problem all day and my code just does not work. My teacher said we do not need to use vector to increase the array size so any tips on how to solve this problem would be great because I just keep hitting a dead end.

C++ dynamic array function help?
C++ does not have any native "dynamic array" capability. The closest thing would be the use of the ANSI realloc() function, but that's a C thing, and not special to C++.


Now you *could* write your own class that manages an array. Let's say your class is something like this:





class Array {


a_class *m_array;


size_t m_nCurrentArraySize;


void Resize(size_t nNewSize);


...


};





Then your implementation might looks like this





void Array::Resize(size_t nNewSize) {


a_class *pNewArray = new a_class[nNewSize];


for (size_t i = 0; i %26lt; m_nCurrentArraySize; i++)


pNewArray[i] = m_array[i];


m_nCurrentArraySize = nNewSize;


delete [] m_array;


m_array = pNewArray;


}
Reply:You can use a linked list for this type of operation but your teacher wanted you to use 'dynamic' arrays. Nothing like making things more difficult.





one_student is assumed to be a structure.


student_class is assumed to be a dynamic array of one_student.





class MyClassArray


{


public:


MyClassArray() {student_enrol = NULL; student_count = 0;}


void AddStudent(one_student %26amp;student);


private:


typedef one_student *student_array;


student_array student_enrol;


int student_count;


...


};





MyClassArray::AddStudent(one_student %26amp;student)


{


student_class temp = new one_student[student_count+1];


for (int i = 0; i %26lt;= student_count; ++i)


temp[i] = student_enrol[i];


temp[student_count++] = student;


delete student_enrol;


student_enrol = temp;


}
Reply:It might be useful for you to paste the code you've got, and tell us specifically what error you're seeing.

garden sheds