Post your C/C++ Programs Here

Status
Not open for further replies.

himanshu1114

Right off the assembly line
Program to print R character in C.Is their anyone to give answer.

I'm getting problem while making R alphabet with * program.
Plz give any solution.
 

nims11

BIOS Terminator
Re: Program to print R character in C.Is their anyone to give answer.

I'm getting problem while making R alphabet with * program.
Plz give any solution.

its easy and basic coordinate geometry. apply the equation of circle to draw the upper half of "R" and the rest part is simple.

here- *codepad.org/lcoB15iZ
i wrote it for you. figure out how i used the circle equation. to increase the size of "R" drawn in the program,just increase the value of variable "upper".
 
Last edited:

Ron

||uLtiMaTE WinNER||
I m learning DATA STRUCTURE WITH C++...
But dont hv any gud material...Does anyone know gud sites for learnin DS
 

Neo

.
Program to mutiply two matrices.

#include<iostream.h>
#include<conio.h>
int main()
{
int mat1[3][3],mat2[3][3],mat3[3][3];
/*entering values in the first array*/
cout<<"enter the numbers of first matrix : "<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cin>>mat1[j];
}
}

/*entering values in the second array*/

cout<<"enter the numbers of second matrix : "<<endl;

for(int k=0;k<3;k++)
{
for(int l=0;l<3;l++)
{
cin>>mat2[k][l];
}
}

/*performing multiplication*/
for(int m=0;m<3;m++)
{
for(int n=0;n<3;n++)
{
mat3[m][n]=0;
for(int o=0;o<3;o++)
{
mat3[m][n]=mat3[m][n]+(mat1[m][o]*mat2[o][n]);
}
}
}

/*printing the result*/

cout<<"product of the matrices is : "<<endl;

for(int s=0;s<3;s++)
{
for(int t=0;t<3;t++)
{
cout<<mat3[t]<<" ";
}
cout<<endl;
}



getch();
}


:):):)

#include<iostream.h>
#include<conio.h>
int main()
{
int mat1[3][3],mat2[3][3],mat3[3][3];
/*entering values in the first array*/
cout<<"enter the numbers of first matrix : "<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cin>>mat1[j];
}
}

/*entering values in the second array*/

cout<<"enter the numbers of second matrix : "<<endl;

for(int k=0;k<3;k++)
{
for(int l=0;l<3;l++)
{
cin>>mat2[k][l];
}
}

/*performing multiplication*/
for(int m=0;m<3;m++)
{
for(int n=0;n<3;n++)
{
mat3[m][n]=0;
for(int o=0;o<3;o++)
{
mat3[m][n]=mat3[m][n]+(mat1[m][o]*mat2[o][n]);
}
}
}

/*printing the result*/

cout<<"product of the matrices is : "<<endl;

for(int s=0;s<3;s++)
{
for(int t=0;t<3;t++)
{
cout<<mat3[t]<<" ";
}
cout<<endl;
}



getch();
}


:):):)
 

Vyom

The Power of x480
Staff member
Admin
14+ YouTube Playlists To Watch & Learn Computer Programming Quickly
Including C++. Might be helpful for learners here.

14+ YouTube Playlists To Watch & Learn Computer Programming Quickly
 

vickybat

I am the night...I am...
Guys i have a simple query: Whilst using linked lists, how to display char strings?

I mean i've written the following code and it displays the last char of each string.

Code:
#include<iostream>
#include <limits>
#include <string>
using namespace std;
struct emp
{
  char name[5];
  struct emp *next;
};
int main()
{
   emp*start =NULL;
   emp emp1 ,emp2,emp3,emp4,emp5;
   start = &emp1;
   emp1.name[5] = 'llana';
   emp1.next = &emp2;
   emp2.name[5] =  'kim';
   emp2.next = &emp3;
   emp3.name[5] = 'lara';
   emp3.next = &emp4;
   emp4.name[5] =  'alice';
   emp4.next = &emp5;
   emp5.name[5] = 'trish';
   emp5.next = NULL;
   
   for (int i=1;i<=5;i++)
   {
       cout<<"The employee names are:"<< start-> name[5]<<endl;
       start =start->next;
   
     
     }
     cin.ignore();
     cin.get();
   
    return 0;  
}

IT GENERATES THE FOLLOWING OUTPUT:

The employee names are : a
The employee names are : m
The employee names are : a
The employee names are : e
The employee names are : h

These are the last characters of the strings. Need to display the full char.
 

Liverpool_fan

Sami Hyypiä, LFC legend
^^ Why are you using name[5] = 'lara', etc.? to save values on the character array string? Since you are coding in C++, I would recommend you to use std::string as the string variable in the class. Also why are you using single quotes for strings? In C/C++ single quotes are for characters, double quotes are for strings.

Something like this. Does this give you the output you desire?

PHP:
#include<iostream>
#include<string>

using namespace std;
struct emp
{
  string name;
  struct emp *next;
};
int main()
{
   emp*start =NULL;
   emp emp1 ,emp2,emp3,emp4,emp5;
   start = &emp1;
   emp1.name = "llana";
   emp1.next = &emp2;
   emp2.name =  "kim";
   emp2.next = &emp3;
   emp3.name = "lara";
   emp3.next = &emp4;
   emp4.name =  "alice";
   emp4.next = &emp5;
   emp5.name = "trish";
   emp5.next = NULL;
   
   for (int i=1;i<=5;i++)
   {
       cout<<"The employee names are:"<< start->name<<endl;
       start =start->next;
   
     
     }
     cin.ignore();
     cin.get();
   
    return 0;  
}
EDIT: Added <string>, dependance on compiler-specific is fail.

And if you keen to use C style string "character arrays", then you use the array name just "name" instead of name[5] to assign values. And yeah you'll probably need somethink like strcpy, or the function in STL which does equivalent of creating a string from array of characters (don't remember it) to copy strings to character arrays. Also note you need to allocate atleast the space needed + 1 char memory to store termination character('\0') with a char array in C/C++. Your program here will need at least 6 characters "alice\0", etc.
And there's no need to use so many headers.
 

Liverpool_fan

Sami Hyypiä, LFC legend
^^I thought that too but it worked regardless
Ideone.com | Online C++ Compiler & Debugging Tool

Anyway, ideally it should be included.
*www.cplusplus.com/forum/beginner/13356/
 

vickybat

I am the night...I am...
@ Liverpool_fan , Neuron

Thanks a lot guys. Yeah the code worked this time. Silly me. I have really forgotten a lot of basics. Btw i'm from electronics and communication background and i'm brushing up my c++.

Might need more help from you guys.:smile:
 

vickybat

I am the night...I am...
Guys wrote a simple code on determining the maximum and minimum number in an array using class concept in c++.

But the exe is crashing when executed. Here's the code:

Code:
include <iostream>
using namespace std;

class compare 
{
      int a[10];
       int i;
       int mx;
       int mn;
      
      public:
             void max();
            
};

   void compare::max()
   {
        cout <<"enter the number:"<<endl;
        cin>> a[i];

        mx = a[0];
        mn = a[0];
        
        for (i=0;i<10;i++)
         {
        if (a[i] > mx)
          {
                mx = a[i];
                }
               else if(a[i]< mn)
		{
			mn = a[i];
		}
     }

}


int main()
{
    compare d1;
    d1.max();
    
    
    cout<<"the max number is:"<< mx<<endl;
    cout<<"the min number is:"<<mn <<endl;
    
    cin.ignore();
     cin.get();

    
    return 0;
    
};

Need a solution.
 

sakumar79

Technomancer
1. The class concept is being used only theoretically here. You are simply putting a procedure into the class and returning the solution...
2. In max procedure, you are inputting a but it is not inside a loop...
3. In main procedure, you are outputting mx and mn, but these are variable of the class compare, how can you directly print them...

IMHO, your class should include a public procedure for inputting the data, a private procedure for running the comparison, public procedures to get max and min... Otherwise, there is nothing different between this program and a program without classes...

Arun
 

vickybat

I am the night...I am...
^^ Okay buddy thanks for your valuable comments.:smile: I finally figured it out:

Code:
#include<iostream>
using namespace std;

class compare
{
  int a[10];
  int max;
  int min;
  int i;

  public:
  
  void get_data();
  void show_data();
  void compute();
};
  

  void compare::get_data()
  {
    
   for (i=0;i<10;i++)

   {
      cout<<"Enter the numbers in the array:"<<endl;
       cin>>a[i];
    }
}

 void compare::show_data()
{

cout<<"The maximum number is:"<<max<<endl;
cout<<"The minimum number is:"<<min<<endl;
}



void compare::compute()

{


        max = a[0];
        min = a[0];
        
        for (i=0;i<10;i++)
         {
        if (a[i] > max)
          {
                max = a[i];
                }
               else if(a[i]< min)
		{
			min = a[i];
		} 

              }

           }

int main()
{
    compare d1;
    d1.get_data();
    d1.compute();
    d1.show_data();
    
    
    cin.ignore();
     cin.get();

    
    return 0;
    
};

Its working.:smile:
 

sakumar79

Technomancer
Thats much better...

A small suggestion - have a boolean variable to check if finding max/min is done - this should be set to false initially, again set to false after receiving input data, and set to true after sorting. Now, in show data module, check if this boolean variable is true (to check if max/min is found) and if not, call the comparing module from inside show data module... This way, if someone calls show data module without running the compare module, it will not give error... Also, if the data is changed after comparing is done, it will know that the data is changed and will recheck the maximum/minimum

Further suggestions:
1. If you want, you can set size of i dynamically by asking number of numbers and assigning array size accordingly. You can have a variable for that also if you want...
2. Depending on requirement, comparing module can be made private...

Do get suggestions from others also... I am personally not a professional programmer though I have more done than 10 years of casual small scale programming

Arun
 

mayoorite

Night Fury
Pattern by a beginner

Code:
#include<iostream.h>
#include<conio.h>
main()
{

	clrscr();
	int i,j,n;
	char ch,l;
	cout<<"Enter an alphabet:";
	cin>>ch;
	l=ch;
	for(i=0;i<=(l-64) && ch<='z' && ch>='A';i++)
       {        for(n=40;n>i;n--)
		cout<<" ";
		for(j=0,ch='A';j<i;j++,ch++)
		cout<<ch<<" ";
		cout<<endl;
       }

	getch();
}
*i51.tinypic.com/ifsrpd.jpg
Please suggest some changes in it to make it more cooler.
:chinscratch:
 
Last edited:
Status
Not open for further replies.
Top Bottom